可怕的java.lang.NoClassDefFoundError [英] The dreaded java.lang.NoClassDefFoundError
问题描述
我已经查看了许多关于这个错误的现有线程,但仍然没有运气。我甚至不想包装罐子或使用任何第三方包装工具。我只是从Eclipse中运行(很棒),然后尝试从命令行运行完全相同的应用程序,在同一个位置(获得此错误)。我的目标是能够压缩bin文件夹并将其发送出去,由别人通过命令行脚本运行。一些细节:
- 这是一个命令行应用程序,我使用commons-lang-2.4.jar作为字符串实用程序。这是无法找到的文件(特定的java.lang.NoClassDefFoundError:org / apache / commons / lang / StringEscapeUtils)
- 我的lib文件夹中有该jar,并添加它通过右键单击构建路径 - >添加到构建路径到Eclipse中的构建路径。
- .classpath文件看起来正确,包含对jar的引用,但我认为文件仅由Eclipse使用(包含以下行:
< classpathentry kind =libpath =lib / commons-lang-2.4.jar/>
) - 这可能与Eclipse工作目录设置有关吗?我有一些内部的模板文件,我创建的是在src / templates下,唯一的方法,我似乎可以看到这些被看到是通过将项目工作目录设置为AppName / src。也许我应该把它们放在别的地方?
如果有任何其他信息有帮助,请告诉我。当然这是一件简单的事情,但是我在这一点上浪费了太多的时间。这是提醒我为什么我原来在'05左右回到Java ...
A noClassDefFoundError
基本上意味着该类在compiletime期间在类路径中,但在运行时在类路径中缺少。
在您的情况下,在命令行中使用 java.exe
执行时,需要在 -cp
或<$ c $中指定类路径c> -classpath 参数。或者如果它是一个JAR文件,那么您需要在其 MANIFEST.MF
class-path 条目中指定它>文件
参数/条目的值可以是包含所有 .class $的文件夹的绝对或相对文件系统路径c $ c>文件或个人
.jar
文件。您可以使用分号;
分隔路径。当路径包含空格时,您需要使用双引号包装特定路径。示例:
code> java -cp。; c:/path/to/file.jar;c:/ spacy path / to / classesmypackage.MyClass
为了节省每次在命令行中输入和编辑参数的努力,请使用 .bat
文件。 p>
编辑:我应该已经意识到你使用的是基于Unix的操作系统,上面的例子是以Windows为目标的例子,在Unix像平台一样,您可以遵循相同的规则,但是您需要使用冒号分隔路径:
而不是最终批处理文件,请使用。 sh
文件。
java -cp。:/ path / to / file.jar: spacy path / to / classesmypackage.MyClass
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
- It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
- I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
- The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line:
<classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>
) - Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError
basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe
from commandline, you need to specify the classpath in the -cp
or -classpath
argument. Or if it is a JAR file, then you need to specify it in the class-path
entry of its MANIFEST.MF
file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class
files or to an individual .jar
file. You can separate paths using a semicolon ;
. When a path contains spaces, you need to wrap the particular path with doublequotes "
. Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat
file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon :
and instead of an eventual batch file, use a .sh
file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
这篇关于可怕的java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!