Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1710
  • Last Modified:

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

0
Juuno
Asked:
Juuno
  • 14
  • 13
  • 4
4 Solutions
 
objectsCommented:
>             realPath = this.getServletContext().getRealPath(".");

should be:

            realPath = this.getServletContext().getRealPath("/");
0
 
objectsCommented:
actually reading again, should be:  ...getRealPath("\WEB-INF\classes");
0
 
objectsCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
JuunoAuthor Commented:
I've aleady tried with classLoader but still, it doesn't give the path. I just get the path: C:\Eclipse.
0
 
objectsCommented:
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
 
JuunoAuthor Commented:
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
 
objectsCommented:
post the code you tried.
0
 
JuunoAuthor Commented:
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
 
objectsCommented:
> 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
 
JuunoAuthor Commented:
> 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
 
objectsCommented:
>             realPath = this.getServletContext().getRealPath(".");

should be:

            realPath = this.getServletContext().getRealPath("/WEB-INF/classes");
0
 
JuunoAuthor Commented:
>   realPath = this.getServletContext().getRealPath("/WEB-INF/classes");
I got NullPointerException, also in this case.
0
 
objectsCommented:
NPE where exactly? Can't see how that line would cause a NPE
0
 
JuunoAuthor Commented:
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
 
JuunoAuthor Commented:
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
 
objectsCommented:
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
 
JuunoAuthor Commented:
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
 
objectsCommented:
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
 
JuunoAuthor Commented:
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
 
objectsCommented:
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
 
CEHJCommented:
>>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
 
JuunoAuthor Commented:
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
 
CEHJCommented:
>>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
 
objectsCommented:
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
 
objectsCommented:
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
 
JuunoAuthor Commented:
@ CEHJ: I got NullPointerException.

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

I suspect the 'conditions' are not right. i.e. the xml file is not present in your running webapp
0
 
JuunoAuthor Commented:
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
 
CEHJCommented:
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
 
objectsCommented:
> @ 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
 
JuunoAuthor Commented:
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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 14
  • 13
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now