NoClassDefFound when trying to run java with exter

2019-02-26 02:32发布

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

4条回答
可以哭但决不认输i
2楼-- · 2019-02-26 02:52

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.

查看更多
乱世女痞
3楼-- · 2019-02-26 02:57

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.

查看更多
等我变得足够好
4楼-- · 2019-02-26 03:00

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.

查看更多
闹够了就滚
5楼-- · 2019-02-26 03:03

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.

查看更多
登录 后发表回答