简体   繁体   English

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

[英]NoClassDefFound when trying to run java with external libraries

I have problem with running my java project. 我在运行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

It's compiled using maven and I end up with .jar file. 它使用maven编译,最终得到.jar文件。 I decided today, that I'll use log4j for logging instead of wimple System.out... Before this change it was working fine 我今天决定,我将使用log4j代替wimple System.out进行日志记录...在此更改之前,它工作正常

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_ADDR指向我的service.jar(带有Launcher类)

SRV_LIBS points to log4j-1.2.16.jar SRV_LIBS指向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. 我还应该提到,我需要在这里某个地方保存log4j库,因为我将把我的应用程序移动到另一台机器上,而且我不知道在那里需要什么库,也许只是纯Java,所以我需要将所有内容都打包到一个包中。 。

If anyone is interested in Launcher class it's very simple: 如果有人对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();
    }
}
}

Does anyone have any idea what am I doing wrong? 有人知道我在做什么错吗? Maybe adding only this one log4j jar file is not enough? 也许仅添加一个log4j jar文件是不够的? I copied it from my maven repository. 我从maven存储库复制了它。

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 EDIT1已经将我指向的jar从log4j-1.2.16-javadoc.jar更改为log4j-1.2.16.jar,但没有帮助

When using java -jar ... , the -classpath argument is ignored . 使用java -jar ...将忽略-classpath参数 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. 在主jar的清单中指定所有必需的jar(但您只能在其中使用相对路径),或者将主jar添加到-classpath并在命令行上提及主类。

Yes, I don't like this, too. 是的,我也不喜欢这样。

SRV_LIBS points to log4j-1.2.16-javadoc.jar SRV_LIBS指向log4j-1.2.16-javadoc.jar

Usually the "-javadoc" in the name means that the jar contains no code - only javadoc documentation. 通常,名称中的“ -javadoc”表示jar不包含任何代码-仅包含Javadoc文档。 Try log4j-1.2.16.jar instead. 请尝试使用log4j-1.2.16.jar。

It looks like you're pointing to the wrong log4j jar. 看来您指向的是错误的log4j jar。

You currently have -classpath server\\libraries\\log4j-1.2.16-javadoc.jar; 您当前拥有-classpath server\\libraries\\log4j-1.2.16-javadoc.jar; which tries to load the javadoc jar file. 它将尝试加载javadoc jar文件。

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

For starters, you are using the log4j javadoc jar. 对于初学者,您正在使用log4j javadoc jar。 That only contains javadoc documentation and not any class files. 那只包含javadoc文档,不包含任何类文件。 You need to be using log4j-1.2.16.jar. 您需要使用log4j-1.2.16.jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM