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\my Applicatio n\WebConte nt\WEB-INF \classes\r esources\.
Now, I am using like this:
File indexfile = new File ("C:\\Documents and Settings\\User\\workspace\ \myApplica tion\\WebC ontent\\WE B-INF\\cla sses\\reso urces\\myI ndex.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.FileNotFoundExcept ion: C:\eclipse\resources\myInd ex.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!!!
C:\Documents and Settings\User\workspace\my
Now, I am using like this:
File indexfile = new File ("C:\\Documents and Settings\\User\\workspace\
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.FileNotFoundExcept
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");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
actually reading again, should be: ...getRealPath("\WEB-INF\c lasses");
as your xml is in the class directory a better approache may be to access it using class loader
URL index = getClass().getResource("/r esources/m yIndex.xml ");
URL index = getClass().getResource("/r
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
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.FileNotFoundExcept ion: C:\Eclipse\resources\myInd ex.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\..
I tried it, but I still got the same error: java.io.FileNotFoundExcept
I tried it with so many ways and all it tried to file th efile in C:\Eclipse\..
post the code you tried.
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");
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
> InputStream indexfile = getClass().getResourceAsSt ream("/res ources/myI ndex.xml") ;
Yes, I got this error : java.lang.NullPointerExcep tion
> 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.
Yes, I got this error : java.lang.NullPointerExcep
> 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");
}
}
> realPath = this.getServletContext().g etRealPath (".");
should be:
realPath = this.getServletContext().g etRealPath ("/WEB-INF /classes") ;
should be:
realPath = this.getServletContext().g
ASKER
> realPath = this.getServletContext().g etRealPath ("/WEB-INF /classes") ;
I got NullPointerException, also in this case.
I got NullPointerException, also in this case.
NPE where exactly? Can't see how that line would cause a NPE
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");
ASKER
When I tried to print that realPath in that servlet, it printed this:
C:\Documents and Settings\User\workspace\.m etadata\.p lugins\org .eclipse.w st.server. core\tmp0\ wtpwebapps \myApplica tion\WEB-I NF\classes
instead of:
C:\Documents and Settings\User\workspace\my Applicatio n\WebConte nt\WEB-INF \classes
C:\Documents and Settings\User\workspace\.m
instead of:
C:\Documents and Settings\User\workspace\my
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.
Its not going to return the location of your project directory, thats not what you want.
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
Besides once you deploy your application the project directory won't even exist
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.d ir")
I can't belive that there's no way to do the same thing in web application.
Thanks anyway!!
If my application is standalone, I can use: System.getProperty("user.d
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.
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("/r esources/m yIndex.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
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
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\ \myApplica tion\\WebC ontent\\WE B-INF\\cla sses\\reso urces\\myI ndex.xml") ;
And what you mean 'the exact set of conditions' ?
Thanks!!
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);
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
@ CEHJ: I got NullPointerException.
@ Object: So, is there noway to load an external files in web applications?
@ 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
I suspect the 'conditions' are not right. i.e. the xml file is not present in your running webapp
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> @ 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
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
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\.plug ins\org.ec lipse.wst. server.cor e\tmp0\wtp webapps\.. .
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!!
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\.plug
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!!