Link to home
Start Free TrialLog in
Avatar of Juuno
Juuno

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually reading again, should be:  ...getRealPath("\WEB-INF\classes");
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");
Avatar of Juuno
Juuno

ASKER

I've aleady tried with classLoader but still, it doesn't give the path. I just get the path: C:\Eclipse.
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

Avatar of Juuno

ASKER

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\..
post the code you tried.
Avatar of Juuno

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Juuno

ASKER

> 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

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

should be:

            realPath = this.getServletContext().getRealPath("/WEB-INF/classes");
Avatar of Juuno

ASKER

>   realPath = this.getServletContext().getRealPath("/WEB-INF/classes");
I got NullPointerException, also in this case.
NPE where exactly? Can't see how that line would cause a NPE
Avatar of Juuno

ASKER

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

Avatar of Juuno

ASKER

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


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.
Avatar of Juuno

ASKER

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.
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
Avatar of Juuno

ASKER

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!!
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.
>>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
Avatar of Juuno

ASKER

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!!
>>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);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Juuno

ASKER

@ CEHJ: I got NullPointerException.

@ Object: So, is there noway to load an external files in web applications?
>>@ CEHJ: I got NullPointerException.

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

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> @ 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
Avatar of Juuno

ASKER

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!!