Reputation: 177
I've created a Java web service with Eclipse that was working excellently yesterday but now all of a sudden Scanner
can't locate the file even though it's in the same directory!
I've tried finding the absolute path of it and providing the exact path but still it insists it cannot find it. However, if I just run it as a Java application it finds it! Am I doing something wrong in terms of trying to read a file with a web service?
The code I would have goes like this:
try {
File file = new File("./data.txt");
Scanner sc = new Scanner(file);
String[] arr = new String[3];
while(sc.hasNextLine()) {
String s = sc.nextLine();
arr = s.split(",");
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
The error I keep getting when running as a web service is
file foundjava.io.FileNotFoundException: ./data.txt (The system cannot fin
d the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at PasswordReset.passwordReset(PasswordReset.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397
)
at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:1
86)
at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:
32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:
327)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase
.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171
)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102
)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Proce
ssor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(Abstrac
tProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:
314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.ja
va:61)
at java.lang.Thread.run(Unknown Source)
Does anyone know what the problem might be?
Upvotes: 2
Views: 322
Reputation: 20430
(Posted on behalf of the OP).
Solved it. Just used ClassLoader and getResource() to actually find the file for Scanner to use and it worked!
Upvotes: 0
Reputation: 33
You are searching for the file in the wrong path. Or the file isnt created when the program starts. Please verify this two solutions, times ago a had the same issue.
Upvotes: 1
Reputation: 127
if you run it as a Java application, "./data.txt" is in the same directory with this java class; if you run it as a web service,the directory is the work directory of server like jboss/jetty/tomcat ...
Upvotes: 0