尝试使用外部库运行java时NoClassDefFound [英] NoClassDefFound when trying to run java with external libraries

查看:1143
本文介绍了尝试使用外部库运行java时NoClassDefFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

这是我如何运行它

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_ADDR points to my service.jar (with Launcher class)

SRV_LIBS指向log4j-1.2.16 .jar

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.

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

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.

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

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();
    }
}
}

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

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 已经改变jar我指的是从log4j-1.2.16-javadoc.jar到log4j-1.2.16.jar但它没有帮助

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

推荐答案

当使用 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.

这篇关于尝试使用外部库运行java时NoClassDefFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆