aspose file tools*
The moose likes Servlets and the fly likes Why I can't open my file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why I can Watch "Why I can New topic
Author

Why I can't open my file?

Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
I have a question about open file at same directory.I have some
files,like follows:
D:\Apache Tomcat
4.0\webapps\login\WEB-INF\classes\GetUserIdentity.class
D:\Apache Tomcat 4.0\webapps\login\WEB-INF\classes\error.txt
The role of GetUserIdentity.class is read data from the file
"error.txt",The file "GetUserIdentity.java" like:
...
try
{
input=new BufferedReader(new FileReader("error.txt"));
...
}
catch(IOException e)
{
e.printStackTrace();
}
But when I start Apache Tomcat 4.0,and I visit index.html which locate
http://localhost:8080/login/servlets/index.html
Then I click OK button,I got errors:
Starting service Tomcat-Standalone
Apache Tomcat/4.0.1
Starting service Tomcat-Apache
Apache Tomcat/4.0.1
java.io.FileNotFoundException: error.txt
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at PreReadError.<init>(PreReadError.java:22)
at GetUserIdentity.<init>(GetUserIdentity.java:20)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Unknown Source)
at
org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:82
0)
at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.jav
a:615)
at
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServl
et.java:396)
at
org.apache.catalina.servlets.InvokerServlet.doPost(InvokerServlet.jav
a:216)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:247)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:193)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:243)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:472)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:201)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:472)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:
2344)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:164)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:566)
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatche
rValve.java:170)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:564)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:170)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:564)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
462)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:564)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:472)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:163)
at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline
.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:472)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at
org.apache.catalina.connector.http.HttpProcessor.process(HttpProcesso
r.java:1011)
at
org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.ja
va:1106)
at java.lang.Thread.run(Unknown Source)
I have put GetUserIdentity.class and error.txt in same dictionary,but
why can't find it?
Any idea will be appreciated!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The problem is in the directory that it is looking for the file. To see the difference you can add code like:

and see the difference in each situation.
Web Applications have some strage ClassLoader effects (and I'm ceratinly not covering them here), but the following code will find the file for you:

The first line uses the current class's ClassLoader to find a resource in the same 'area' called 'error.txt', then we convert it to a File.
hope this helps.
Dave
 
Consider Paul's rocket mass heater.
 
subject: Why I can't open my file?