Solved

getServletContext().getRealPath(".") Error????

Posted on 2009-06-29
31
1,565 Views
Last Modified: 2013-12-02
I want to load xml file from my web application. It is in:
C:\Documents and Settings\User\workspace\myApplication\WebContent\WEB-INF\classes\resources\.

Now, I am using like this:
File indexfile = new File ("C:\\Documents and Settings\\User\\workspace\\myApplication\\WebContent\\WEB-INF\\classes\\resources\\myIndex.xml");

But  giving a path like that is not good, you know. So, I tried to call it by giving the path. I tried to use load-on-startup servlet, and call that 'realPath' variable from my JavaBean. See the code of the servlet please.

But that servlet, instead of giving the path to my application, it gives the path C:\Eclipse. So, I got the error like this:

java.io.FileNotFoundException: C:\eclipse\resources\myIndex.xml (The system cannot find the path specified)

Any idea please. I have been trying this for quite long. And still got no solution.

I use Tomcat and Eclipse.
Really Thanks!!!
public class MyServlet extends HttpServlet {
 

	  public static String realPath;
 

	  public void init(ServletConfig config) throws ServletException {

	    super.init(config);

	    realPath = this.getServletContext().getRealPath(".");

	    System.out.println("Initialization of AppContextServlet");

	  }

	}
 

-------------------------------------- In my java bean, i call like this--------------
 

File resources = new File(MyServlet.realPath, "resources");

File indexfile = new File(resources, "myIndex.xml");

Open in new window

0
Comment
Question by:Juuno
  • 14
  • 13
  • 4
31 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
Comment Utility
>             realPath = this.getServletContext().getRealPath(".");

should be:

            realPath = this.getServletContext().getRealPath("/");
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
actually reading again, should be:  ...getRealPath("\WEB-INF\classes");
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
as your xml is in the class directory a better approache may be to access it using class loader

URL index = getClass().getResource("/resources/myIndex.xml");
0
 

Author Comment

by:Juuno
Comment Utility
I've aleady tried with classLoader but still, it doesn't give the path. I just get the path: C:\Eclipse.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
the classloader doesn't give you a path (you don't need one). It gives a URL which you can then use to open the resource

0
 

Author Comment

by:Juuno
Comment Utility
I do not get what you mean. Can you explain a bit more detail?
I tried it, but I still got the same error: java.io.FileNotFoundException: C:\Eclipse\resources\myIndex.xml (The system cannot find the path specified) .

I tried it with so many ways and all it tried to file th efile in C:\Eclipse\..
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
post the code you tried.
0
 

Author Comment

by:Juuno
Comment Utility
I 've tried all these ways:
URL index = getClass().getResource("/resources/myIndex.xml");

File indexfile = new File(index + "myIndex.xml");
 

-------------------------------------------

InputStream indexfile = getClass().getResourceAsStream("/resources/myIndex.xml");
 

--------------------------------------------

ClassLoader loader = this.getClass().getClassLoader();

InputStream indexfile  = loader.getResourceAsStream("resources/myIndex.xml");
 

-------------------------------------------

//the one using HttpServlet

File resources = new File(MyServlet.realPath, "resources");

File indexfile = new File(resources, "myIndex.xml");

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
Comment Utility
> File indexfile = new File(index + "myIndex.xml");

you can't create a file from a url

> InputStream indexfile = getClass().getResourceAsStream("/resources/myIndex.xml");

that should work, assuming its the right classloader. Is it returning null?
what container are you using?

> InputStream indexfile  = loader.getResourceAsStream("resources/myIndex.xml");

should be /resources

> File resources = new File(MyServlet.realPath, "resources");

that should work, if you create realPath as I suggested in earlier post
0
 

Author Comment

by:Juuno
Comment Utility
> InputStream indexfile = getClass().getResourceAsStream("/resources/myIndex.xml");
Yes, I got this error : java.lang.NullPointerException

> File resources = new File(MyServlet.realPath, "resources");
Please check the code snippet. I think it is what u said. But it returns: C:\Eclipse\.. not the real path.

I use Tomcat.
public class MyServlet extends HttpServlet {

 

          public static String realPath;

 

          public void init(ServletConfig config) throws ServletException {

            super.init(config);

            realPath = this.getServletContext().getRealPath(".");

            System.out.println("Initialization of AppContextServlet");

          }

        }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
>             realPath = this.getServletContext().getRealPath(".");

should be:

            realPath = this.getServletContext().getRealPath("/WEB-INF/classes");
0
 

Author Comment

by:Juuno
Comment Utility
>   realPath = this.getServletContext().getRealPath("/WEB-INF/classes");
I got NullPointerException, also in this case.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
NPE where exactly? Can't see how that line would cause a NPE
0
 

Author Comment

by:Juuno
Comment Utility
When I execute the following code to get that 'realPath'.
File resources = new File(MyServlet.realPath, "resources");

File indexfile = new File(resources, "myIndex.xml");

Open in new window

0
 

Author Comment

by:Juuno
Comment Utility
When I tried to print that realPath in that servlet, it printed this:

C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myApplication\WEB-INF\classes

instead of:
C:\Documents and Settings\User\workspace\myApplication\WebContent\WEB-INF\classes


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
Comment Utility
thats correct, its returning the location of the running application.
Its not going to return the location of your project directory, thats not what you want.
0
 

Author Comment

by:Juuno
Comment Utility
Then, how should I do to get my project directory so that I can load my data files without giving the absolute path in my java program.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you don't :) Getting your project directory location is pointless, eclipse has deployed a copy of your web application and is running that.
Besides once you deploy your application the project directory won't even exist
0
 

Author Comment

by:Juuno
Comment Utility
Then, how can I get the local file without giving it the real path.
If my application is standalone, I can use: System.getProperty("user.dir")
I can't belive that there's no way to do the same thing in web application.

Thanks anyway!!
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you're not trying to access a local file, you are trying to access a file in your webapps classes directory.
you need to access the files in your webapp, *not* your project.

Accessing the project does not make sense, its not even going to exist when you deploy the application.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>URL index = getClass().getResource("/resources/myIndex.xml");

should be fine. Just make sure that you have directory 'resources' under WEB-INF/classes, containing the xml file wherever it's running. I suspect you have not had that exact set of conditions
0
 

Author Comment

by:Juuno
Comment Utility
Yeah, sure there is directory 'resources' under WEB-INF/classes folder since I can call it using  this: File indexfile = new File ("C:\\Documents and Settings\\User\\workspace\\myApplication\\WebContent\\WEB-INF\\classes\\resources\\myIndex.xml");

And what you mean 'the exact set of conditions' ?

Thanks!!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>And what you mean 'the exact set of conditions' ?

I just mean its being present all the time in all deployments

>>Yeah, sure there is directory 'resources' under WEB-INF/classes folder since I can call it using  this

You should be able to get a url using the code i quoted normally. What happens? Please post result of

out.println(index);
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
Comment Utility
Your application isn't using that directory and you shouldn't be trying to use it, it is using:

C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myApplication\WEB-INF\classes

which the code I suggested above is correctly returning.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
And if you do use that directory then you application will break as soon as you deploy it.
If the options I suggested are not working then the likely reason is that the xml file is not getting included in your application, for example because it is not in a source folder.
0
 

Author Comment

by:Juuno
Comment Utility
@ CEHJ: I got NullPointerException.

@ Object: So, is there noway to load an external files in web applications?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>@ CEHJ: I got NullPointerException.

I suspect the 'conditions' are not right. i.e. the xml file is not present in your running webapp
0
 

Author Comment

by:Juuno
Comment Utility
Hmmmm.... I get really confused!!

So, where exactly is the running webapp. Where can I put those xml files to be seen and loaded using relative path from my web application?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
Comment Utility
It's under

C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myApplication

Try adding the resources directory and its content under your sources root
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> @ Object: So, is there noway to load an external files in web applications?

yes, but what you're trying to do is load a file internal to the webapp

> So, where exactly is the running webapp

In the directory I already posted earlier

> Where can I put those xml files to be seen and loaded using relative path from my web application?

put the resources folder in a source folder in your project
0
 

Author Comment

by:Juuno
Comment Utility
Sorry for my late response. I was on a trip so that I couldn't reply.

I got it. It only copies the files and folders under WebContent and just only the real classes folder under the WEB-INF\classes folder.

So, I move my resources folder under WebContent and so it copies it to \workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\...

And then I load the file using Servlet, what u described earlier.
Anyway, you guys helped me out a lot and I got to know that coz all of ur posts.
I really appreciate for that.
Thanks a lot!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now