刚刚有了一个关于我有一个奇怪的运行时错误的问题。 我有当由外部源提示JSON对象响应一个JSON服务器的工作。 截至目前,然而,我只是试图让JSON对象并发送出去之前运行。 我的代码编译没有指定任何classpath中,但是当涉及到运行时,它抛出一个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.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.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$1.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
我的代码如下位置:
import 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");
}
}
}
和JSON文件夹是在同一目录ExampleServer.java
,与JSON类居住在net/sf/json
在我进口指定。 为什么不工作,直到运行? 是我的类路径不对因某种原因? 什么是对包括必要的classpath适当的语法?
提前致谢