Java Defclass,尝试使用外部库运行java时NoClassDefFound

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值