gpt4 book ai didi

Java 运行时 ClassNotFoundException

转载 作者:搜寻专家 更新时间:2023-11-01 04:00:06 24 4
gpt4 key购买 nike

只是对我遇到的一个奇怪的运行时错误有疑问。我正在使用一个 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.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 中,如我的导入中所指定。为什么直到运行时它才工作?我的类路径是否出于某种原因不正确?包含必要类路径的适当语法是什么?

提前致谢

最佳答案

来自 json-lib homepage :

Json-lib requires (at least) the following dependencies in your classpath:

  1. jakarta commons-lang 2.5
  2. jakarta commons-beanutils 1.8.0
  3. jakarta commons-collections 3.2.1
  4. jakarta commons-logging 1.1.1
  5. ezmorph 1.0.6

您的实现编译仅表明编译器可以找到正确的类和方法。然而,这些类和方法的实现可能取决于其他库。

可以找到有关项目依赖项的更多信息here .

关于Java 运行时 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com