尝试使用外部库运行 java 时出现 NoClassDefFound

发布于 2024-10-27 19:57:51 字数 2476 浏览 3 评论 0原文

我的 java 项目运行时遇到问题。

E:\Workspace\compiled>java -classpath server\libraries\log4j-1.2.16.jar;E:\Workspace\compiled\server\service-1.0-SNAPSHOT.jar;E:\Workspace\compiled\server\libraries\log4j-1.2.16.jar -Djava.security.policy=E:\Workspace\compiled\socket.policy -Djava.rmi.server.codebase=file:///E:\Workspace\compiled\server\service-1.0-SNAPSHOT.jar -jar E:\Workspace\compiled\server\service-1.0-SNAPSHOT.jar E:\Workspace\compiled\log4j.properties
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/PropertyConfigurator
    at pwr.mgr.server.service.Launcher.main(Launcher.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.PropertyConfigurator
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

它是使用 maven 编译的,最终得到 .jar 文件。 我今天决定,我将使用 log4j 进行日志记录,而不是 wimple System.out... 在此更改之前,它工作正常,

这是我运行它的方式

java -cp .;%SRV_ADDR%;%SRV_LIBS% -Djava.security.policy=%POLICY_FILE% -Djava.rmi.server.codebase=file:///%SRV_ADDR% pwr.mgr.server.service.Launcher %CD%\log4j.properties

SRV_ADDR 指向我的 service.jar (带有 Launcher 类)

SRV_LIBS 指向 log4j-1.2.16.jar

我添加了“.;”一开始希望它会有所帮助,因为提到的文件位于我尝试启动此文件的子目录中。

我还应该提到,我需要 haev log4j 库在这里的某个地方,因为我将把我的应用程序移动到另一台机器上,并且我不知道在那里需要什么库,也许只有纯 java,所以我需要在一个包中提供所有内容。

如果有人对 Launcher 类感兴趣,那很简单:

public class Launcher {

public static void main(String args[]) {
    PropertyConfigurator.configure(args[0]);
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }

    try {
        Runtime.getRuntime().exec("rmiregistry");
        ServerIntf obj = new Server();
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind("Server", obj);
        System.out.println("Server bound in registry");
    } catch (Exception e) {
        System.out.println("Server err: " + e.getMessage());
        e.printStackTrace();
    }
}
}

有人知道我做错了什么吗?也许只添加这个 log4j jar 文件还不够?我从我的 Maven 存储库复制了它。

EDIT1 已经将我指向的 jar 从 log4j-1.2.16-javadoc.jar 更改为 log4j-1.2.16.jar 但没有帮助

I have problem with running my java project.

E:\Workspace\compiled>java -classpath server\libraries\log4j-1.2.16.jar;E:\Workspace\compiled\server\service-1.0-SNAPSHOT.jar;E:\Workspace\compiled\server\libraries\log4j-1.2.16.jar -Djava.security.policy=E:\Workspace\compiled\socket.policy -Djava.rmi.server.codebase=file:///E:\Workspace\compiled\server\service-1.0-SNAPSHOT.jar -jar E:\Workspace\compiled\server\service-1.0-SNAPSHOT.jar E:\Workspace\compiled\log4j.properties
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/PropertyConfigurator
    at pwr.mgr.server.service.Launcher.main(Launcher.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.PropertyConfigurator
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

It's compiled using maven and I end up with .jar file.
I decided today, that I'll use log4j for logging instead of wimple System.out...
Before this change it was working fine

here's how I run it

java -cp .;%SRV_ADDR%;%SRV_LIBS% -Djava.security.policy=%POLICY_FILE% -Djava.rmi.server.codebase=file:///%SRV_ADDR% pwr.mgr.server.service.Launcher %CD%\log4j.properties

SRV_ADDR points to my service.jar (with Launcher class)

SRV_LIBS points to log4j-1.2.16.jar

I added ".;" at the begining hoping it would help, because mentioned files are in child directory to the one from I am trying to launch this.

I should also mention that I need to haev log4j library somewhere here, because I will be moving my app onto another machine and I don't know what libraries to expect there, maybe only pure java, so I need to deliver everything in one package.

If anyone is interested in Launcher class it's very simple:

public class Launcher {

public static void main(String args[]) {
    PropertyConfigurator.configure(args[0]);
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }

    try {
        Runtime.getRuntime().exec("rmiregistry");
        ServerIntf obj = new Server();
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind("Server", obj);
        System.out.println("Server bound in registry");
    } catch (Exception e) {
        System.out.println("Server err: " + e.getMessage());
        e.printStackTrace();
    }
}
}

Does anyone have any idea what am I doing wrong? Maybe adding only this one log4j jar file is not enough? I copied it from my maven repository.

EDIT1 already changed jar i'm pointing to from log4j-1.2.16-javadoc.jar to log4j-1.2.16.jar but it didn't help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

著墨染雨君画夕 2024-11-03 19:57:51

使用java -jar ...时,-classpath 参数被忽略。要么在主 jar 的清单中指定所有必需的 jar(但只能使用相对路径),要么将主 jar 添加到 -classpath 并在命令行中提及主类。

是的,我也不喜欢这个。

When using java -jar ..., the -classpath argument is ignored. Either specify all necessary jars in the manifest of your main jar (but you can only use relative paths there), or add your main jar to the -classpath and mention the main class on the command line.

Yes, I don't like this, too.

千寻… 2024-11-03 19:57:51

SRV_LIBS指向log4j-1.2.16-javadoc.jar

通常名称中的“-javadoc”表示该 jar 不包含代码 - 仅包含 javadoc 文档。尝试使用 log4j-1.2.16.jar 代替。

SRV_LIBS points to log4j-1.2.16-javadoc.jar

Usually the "-javadoc" in the name means that the jar contains no code - only javadoc documentation. Try log4j-1.2.16.jar instead.

热风软妹 2024-11-03 19:57:51

您似乎指向了错误的 log4j jar。

您目前有
-classpath server\libraries\log4j-1.2.16-javadoc.jar;
它尝试加载 javadoc jar 文件。

尝试使用 -classpath server\libraries\log4j-1.2.16.jar; 代替。

It looks like you're pointing to the wrong log4j jar.

You currently have
-classpath server\libraries\log4j-1.2.16-javadoc.jar;
which tries to load the javadoc jar file.

Try using -classpath server\libraries\log4j-1.2.16.jar; instead.

知足的幸福 2024-11-03 19:57:51

对于初学者,您使用的是 log4j javadoc jar。它只包含 javadoc 文档,不包含任何类文件。您需要使用 log4j-1.2.16.jar。

For starters, you are using the log4j javadoc jar. That only contains javadoc documentation and not any class files. You need to be using log4j-1.2.16.jar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文