从servlet在jar中运行类时的FileNotFoundException

时间:2012-04-26 23:20:29

标签: java-ee servlets filenotfoundexception

在eclipse IDE中,有一个独立的java应用程序可以读取文件。文件都在项目下的文件夹中。
我创建了另一个Web应用程序并编写了一个servlet,并在WEB_INF / lib中添加了上述应用程序的jar文件,以便我可以使用其中的所有类.servlet非常简单但java应用程序无法读取文件到早些时候。我得到java.io.FileNotFoundException(系统找不到指定的路径) 我检查了jar文件,它包含了包含要读取的文件的文件夹。我甚至尝试将文件夹添加到TOMCAT中的类路径中。但我仍然无法摆脱那个错误。有人能告诉我什么是错的吗?

我收到错误的地方

public class KnowledgeSummaryServlet extends HttpServlet {

Answerer a = new Answerer();

类Answerer就是这样的

public class Answerer {
Lookup lvg  = new Lookup("resources/test/xyz", "resources/test/test.txt");

这是我运行servlet后的错误跟踪

java.io.FileNotFoundException: resources\test\xyz (The system cannot              
find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:656)
at com.Lookup.localInitialize(Lookup.java:49)
at com.Lookup.<init>(Lookup.java:44)
at com.Answerer.<init>(Answerer.java:27)
at com.web.KnowledgeSummaryServlet.<init>(KnowledgeSummaryServlet.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:799)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:2)

您不应使用FileInputStream将类路径资源作为本地磁盘文件系统资源。这毫无意义。使用ClassLoader将类路径资源作为类路径资源获取。

鉴于您在servlet中并假设/resources文件夹是类路径的一部分,请按以下方式获取它们:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream xyzInput = classLoader.getResourceAsStream("test/xyz");
InputStream txtInput = classLoader.getResourceAsStream("test/test.txt");
// ...