Servlet 在路径中的类时抛出 java.lang.ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6636643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet Throwing java.lang.ClassNotFoundException when class in path
提问by djTeller
I am writing a simple servlet and trying to create an instance of one of my classes, DataStore
, in the code.
我正在编写一个简单的 servlet 并尝试在代码中创建我的一个类的实例DataStore
。
This class is public and sits in a file called DataStore.java
in the same package as the Servlet code.
此类是公共的,位于DataStore.java
与 Servlet 代码相同的包中调用的文件中。
When I try to create a new instance in the code:
当我尝试在代码中创建一个新实例时:
DataStore dStore = new DataStore();
I get the following exception:
我收到以下异常:
java.lang.ClassNotFoundException: backend.DataStore
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at backend.AjaxServlet.createGame(AjaxServlet.java:196)
I tried creating an instance of this class in a differnet file and it worked great. Any idea what could be the cause of this?
我尝试在不同的网络文件中创建此类的实例,并且效果很好。知道这可能是什么原因吗?
回答by AlexR
If JVM throws ClassNotFoundException the class is not found in classpath. It means that something is not configured correctly.
如果 JVM 抛出 ClassNotFoundException,则在类路径中找不到该类。这意味着某些东西没有正确配置。
So, check the class (i mean file backend.DataStore.class
). It's path should be: your web application folder/WEB-INF/classes/backend/DataStore.class
. If this class is packaged into separate jar file this jar must be under YOUR_WEB_APP/WEB-INF/lib/yourjar.jar
所以,检查类(我的意思是文件backend.DataStore.class
)。它的路径应该是:你的web application folder/WEB-INF/classes/backend/DataStore.class
. 如果这个类被打包成单独的jar文件,这个jar必须在YOUR_WEB_APP/WEB-INF/lib/yourjar.jar