Solved

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

Posted on 2009-06-29
31
1,620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 13
  • 4
31 Comments
 
LVL 92

Accepted Solution

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

should be:

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

Expert Comment

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

Expert Comment

by:objects
ID: 24741296
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Juuno
ID: 24741321
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
ID: 24741340
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
ID: 24741360
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
ID: 24741392
post the code you tried.
0
 

Author Comment

by:Juuno
ID: 24741418
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
ID: 24741447
> 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
ID: 24741479
> 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
ID: 24741484
>             realPath = this.getServletContext().getRealPath(".");

should be:

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

Author Comment

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

Expert Comment

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

Author Comment

by:Juuno
ID: 24741517
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
ID: 24741533
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
 
LVL 92

Expert Comment

by:objects
ID: 24741548
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
ID: 24741559
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
ID: 24741587
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
ID: 24741636
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
ID: 24741689
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
ID: 24743512
>>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
ID: 24744030
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
ID: 24744046
>>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
ID: 24744055
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
ID: 24744126
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
ID: 24744158
@ CEHJ: I got NullPointerException.

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

Expert Comment

by:CEHJ
ID: 24744173
>>@ 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
ID: 24744187
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
ID: 24744201
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
ID: 24744279
> @ 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
ID: 24776860
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 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