运行jar时java.lang.NoClassDefFoundError [英] java.lang.NoClassDefFoundError while running jar

查看:292
本文介绍了运行jar时java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SLF4J记录器。 jar 文件放在 ../ lib 目录中。应用程序是使用maven和Netbeans创建的。但是我想自己动手去教自己。



应用程序是用命令编译的(当然在分号后面没有空格 - 为了更好的可读性,我添加了它们):

  javac -cp ../lib/poi-3.10-FINAL-20140208.jar; 
../lib/slf4j-api-1.7.6.jar;
../lib/slf4j-log4j12-1.7.6.jar;
../lib/commons-io-1.4.jar
-d target / classes src\main\java\pl\alden\convertcosts\ * .java

我可以用

 <运行它code> java -cp ../lib/poi-3.10-FINAL-20140208.jar; 
../lib/slf4j-api-1.7.6.jar;
../lib/slf4j-log4j12-1.7.6.jar;
../lib/commons-io-1.4.jar;
../lib/log4j-1.2.17.jar;
目标/类
pl / alden / convertcosts / App

我知道我可以将所有jar文件设置为cl classpath 系统变量(Windows 7)。



我想将所有内容放入 jar 并从此 jar 运行它。要创建 jar 我使用

  jar cvfm convertcosts.jar manifest.mf -C target / classes /。 

我有正确的 jar jar tf convertcosts.jar 给出

  META-INF / 
META -INF / MANIFEST.MF
.netbeans_automatic_build
log4j.properties
pl /
pl / alden /
pl / alden / convertcosts /
pl / alden /convertcosts/App.class
pl / alden / convertcosts / ConvertCostsException.class
...

现在我想从 bat 文件运行应用程序:

  set classpath = .. / lib / poi-3.10-FINAL-20140208.jar; 
../lib/slf4j-api-1.7.6.jar;
../lib/slf4j-log4j12-1.7.6.jar;
../lib/commons-io-1.4.jar;
../lib/log4j-1.2.17.jar
java -jar convertcosts.jar

因此我有

 线程main中的异常java.lang.NoClassDefFoundError:
org / slf4j / LoggerFactory
at pl.alden.convertcosts.App。< clinit>(App.java:10)

对我看来,主 jar 文件看不到库 jar 文件。 / p>

我应该改变什么?

解决方案

最好坚持使用显式命令行类路径定义:

  java -cp ../lib/poi-3.10-FINAL-20140208.jar; 
../lib/slf4j-api-1.7.6.jar;
../lib/slf4j-log4j12-1.7.6.jar;
../lib/commons-io-1.4.jar;
../lib/log4j-1.2.17.jar
-jar convertcosts.jar

定义类路径 CLASSPATH 环境变量具有系统范围的影响,您应该知道并非所有java应用程序需要在其类路径上使用这些特定的jar。如果你真的必须,那么在定义 CLASSPATH ../ lib / c $ c>。


I use SLF4J logger. jar file is placed in ../lib directory. Application was created using maven and in Netbeans. But I want to do all by myself to teach myself.

Application is compiled with command (of course there are no spaces after semicolons - I added them for better readability):

javac -cp ../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar 
-d target/classes src\main\java\pl\alden\convertcosts\*.java

and I can run it with

java -cp ../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar;
  ../lib/log4j-1.2.17.jar;
  target/classes
  pl/alden/convertcosts/App

I know I can set all jar files as clclasspath system variable (Windows 7).

I want to put all into jar and run it from this jar. To create jar I use

jar cvfm convertcosts.jar manifest.mf -C target/classes/ .

And I have proper jar: jar tf convertcosts.jar gives

META-INF/
META-INF/MANIFEST.MF
.netbeans_automatic_build
log4j.properties
pl/
pl/alden/
pl/alden/convertcosts/
pl/alden/convertcosts/App.class
pl/alden/convertcosts/ConvertCostsException.class
...

Now I want to run application from bat file:

set classpath=../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar;
  ../lib/log4j-1.2.17.jar
java -jar convertcosts.jar

As a result I have

Exception in thread "main" java.lang.NoClassDefFoundError: 
  org/slf4j/LoggerFactory
  at pl.alden.convertcosts.App.<clinit>(App.java:10)

To me it looks like main jar file does not see library jar files.

What should I change?

解决方案

Better to stick with the explicit command-line classpath definition:

java -cp ../lib/poi-3.10-FINAL-20140208.jar;
  ../lib/slf4j-api-1.7.6.jar;
  ../lib/slf4j-log4j12-1.7.6.jar;
  ../lib/commons-io-1.4.jar;
  ../lib/log4j-1.2.17.jar
  -jar convertcosts.jar

Defining classpath or CLASSPATH environment variable has a system-wide affect and you should be aware that not all java applications need these particular jars on their classpath. If you really must, then at the very least use the full paths instead of ../lib/ when defining the value of the CLASSPATH.

这篇关于运行jar时java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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