Java 运行时 ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11567383/
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
Java runtime ClassNotFoundException
提问by Shelby Vanhooser
Just had a question about an odd runtime error that I'm having. I'm working with a JSON server that responds with a JSON object when prompted by an outside source. As of right now, however, I'm simply trying to get the JSON object up and running before sending it out. My code compiles without specifying any classpath, but when it comes to runtime, it throws a NoClassDefFoundError
as seen here:
刚刚有一个关于我遇到的奇怪的运行时错误的问题。我正在使用一个 JSON 服务器,当外部源提示时,该服务器以 JSON 对象进行响应。然而,就目前而言,我只是尝试在发送 JSON 对象之前启动并运行它。我的代码编译时没有指定任何类路径,但是当涉及到运行时,它会抛出一个NoClassDefFoundError
如下所示的:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.accessimport java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.*;
import net.sf.json.*;
public class ExampleServer{
private DataOutputStream output;
private DataInputStream input;
private ServerSocket server;
private Socket connection;
public static void main(String[] args){
new ExampleServer().runServer();
}
public void ExampleServer(){
System.out.println("Server object created");
}
public void runServer(){
try{
server = new ServerSocket(12345,100);
while(true){
try{
waitForConnection();
getStreams();
processConnection();
sendData("Text");
} catch(Exception e){
System.out.println("Server Terminated Exception.");
} finally {
closeConnection();
}
}
} catch(Exception e){
e.printStackTrace();
}
}
private void waitForConnection(){
try{
System.out.println("Waiting for connection.");
connection = server.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
} catch (IOException e){
System.out.println(e);
}
}
private void getStreams() throws IOException{
output = new DataOutputStream(connection.getOutputStream());
input = new DataInputStream(connection.getInputStream());
System.out.println("Got streams");
}
private void processConnection() throws IOException{
sendData("This is a test message");
}
private void closeConnection(){
try{
if(output != null && connection != null){
sendData("SEVER>>> TERMINATE");
output.close();
connection.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
private void sendData(String message){
try{
JSONObject g = new JSONObject();
g.put("message", message);
System.out.println(g.toString());
} catch (Exception e){
System.out.println("Error writing object");
}
}
0(URLClassLoader.java:58)
at java.net.URLClassLoader.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at ExampleServer.sendData(ExampleServer.java:76)
at ExampleServer.runServer(ExampleServer.java:30)
at ExampleServer.main(ExampleServer.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 15 more
My code follows here:
我的代码如下:
##代码##}
}
and the JSON folder is in the same directory as ExampleServer.java
, with the JSON classes residing in net/sf/json
as specified in my imports. Why is it not working until runtime? Is my classpath not right for some reason? What would be the appropriate syntax for including a necessary classpath?
并且 JSON 文件夹与 位于同一目录中ExampleServer.java
,JSON 类net/sf/json
位于我的导入中指定的位置。为什么它直到运行时才工作?由于某种原因,我的类路径不正确吗?包含必要的类路径的适当语法是什么?
Thanks in advance
提前致谢
回答by Maarten Bodewes
From json-lib homepage:
Json-lib requires (at least) the following dependencies in your classpath:
- jakarta commons-lang 2.5
- jakarta commons-beanutils 1.8.0
- jakarta commons-collections 3.2.1
- jakarta commons-logging 1.1.1
- ezmorph 1.0.6
Json-lib 需要(至少)类路径中的以下依赖项:
- 雅加达公共语言 2.5
- 雅加达公地-beanutils 1.8.0
- 雅加达公共收藏 3.2.1
- 雅加达公共日志 1.1.1
- 变形金刚 1.0.6
That your implementation compiles only shows that the compiler can find the right classes and methods. The implementation of those classes and methods may however depend on other libraries.
您的实现编译仅表明编译器可以找到正确的类和方法。然而,这些类和方法的实现可能依赖于其他库。
More information on project dependencies can be found here.
可以在此处找到有关项目依赖项的更多信息。