更新到Java 7后的NoClassDefFoundError [英] NoClassDefFoundError after updating to Java 7

查看:135
本文介绍了更新到Java 7后的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行项目的JAR文件后得到NoClassDefFoundError。
使用Eclipse进行调试工作正常,但每当我使用windows命令 java -jar myproject.jar 时,我都会收到此错误,因为我安装了JDK 1.7。

I'm getting the NoClassDefFoundError after executing my project's JAR file. Debugging with Eclipse works fine, but I get this error whenever I use the windows command java -jar myproject.jar since I installed JDK 1.7.

使用Java 1.6工作正常。

It was working fine using Java 1.6.

这是错误日志:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout
at program.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
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

我用Google搜索但找不到合适的解决方案。

I googled about that but couldn't find the right solution.

我已经做了什么:
-unins高Java 1.7,安装1.6而不是
- 检查另一台计算机上的类路径
-try(从中卸载Java 1.7之后)
-创建一个空项目,其中一个引用log4j库= >同样的问题

What I already did : -uninstall Java 1.7, install 1.6 instead -check the classpath -try on an other computer (after uninstalling Java 1.7 from it) -create an empty project with one reference to the log4j library => same problem

这里是类路径文件:

<?xml version="1.0" encoding="UTF-8"?>
<classpath> 
<classpathentry kind="src" path="src"/> 
<classpathentry kind="con"path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/RXTXcomm.jar"/> 
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/> 
<classpathentry kind="output" path="bin"/> 
</classpath>

你知道我还应该做些什么吗?

Do you have any idea what else I should do?

谢谢,

Nils

编辑:如果我将项目导出为可运行的JAR 而不是简单的JAR,程序启动但无法使用我正在使用的另一个库(RxtxComm)。这是我得到的日志:

EDIT : if I export the project as an "runnable JAR" instead of a simple "JAR", the program starts but fail to use another library I'm using (RxtxComm). Here's the log I get:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver

EDIT2:我终于通过将rxtxSerial.dll放在Windows / system32中解决了最后一个问题夹。
但是,如果我将项目导出为JAR文件以及为什么我现在需要使用dll文件,我仍然不明白为什么它不再起作用。

I finally resolved the last problem by putting the rxtxSerial.dll in the Windows/system32 folder. But still, I don't understand why it doesn't work anymore if I export my project as a JAR file and why I need to use the dll file now.

推荐答案

在运行jar文件时,你确定log4j的jar在类路径中吗?检查:

Are you sure log4j's jar is in the classpath when running your jar file? Check that either:


  • 你有一个CLASSPATH系统变量,其中包含log4j的jar所在的文件夹

  • you have a CLASSPATH system variable which contains, amongst others, the folder wher log4j's jar is located

启动jar时将该log4j jar文件夹作为参数传递

you pass that log4j jar folder as an argument when launching the jar

java -jar yourJar.jar -classpath c:\ myLog4JJarFolder

java -jar yourJar.jar -classpath c:\myLog4JJarFolder

更新:

根据Andrew Thompson的注释,使用-jar参数会使你的-classpath参数无用(它将被忽略)。为了能够在命令行指定类路径,您还必须使用主类的完全限定名作为启动参数:

As per Andrew Thompson's comment using the -jar argument will make your -classpath argument useless (it will be ignored). To be able to specify the classpath at the command line you have to also use the fully qualified name of your main class as a launch argument:

java -classpath "c:\dir1;c:\dir2" package.subPackage.MainClass

这篇关于更新到Java 7后的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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