从命令行运行时出现 NoClassDefFoundError
我想为我的 Java 程序使用外部库(例如 Google 的 Guava)。我使用 Eclipse,所以我下载了 Guava 的 jar(和源代码)并按照 添加 Java库到项目类路径,以将其添加到 Eclipse 和我的项目的构建路径中。
这工作正常:我可以从 Eclipse 和从 Eclipse 导出的可运行 jar 运行该程序,但是当我尝试直接从 bin/
目录运行时,我会收到错误,就像我以前所做的那样前:
线程“main”中出现异常
java.lang.NoClassDefFoundError:com/google/common/base/Joiner
我该怎么办?
I would like to use an external library (e.g. Google's Guava) for my Java program. I use Eclipse, so I downloaded Guava's jar (and source) and followed Adding a Java library to the project classpath to add it to Eclipse and to the buildpath of my project.
This works fine: I can run the program from Eclipse and from the runnable jar I export from Eclipse, but I get an error when I try to run directly from the bin/
dir, as I used to do before:
Exception in thread "main"
java.lang.NoClassDefFoundError: com/google/common/base/Joiner
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您直接从项目 bin 目录运行类文件,那么您可能必须手动指定类路径:
<代码>C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses MyClassHere
If you're running the class file directly from the project bin directory then you may have to specify the classpath manually:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses MyClassHere
你必须告诉 Java 在哪里可以找到库:
或者如果你想使用 jar 文件,你可以在 MANIFEST 中设置库路径
请查看此处了解说明。
Youll have to tell Java where to find the library:
or if you wanna use a jar file you can set the library path in the MANIFEST
check here for an explanation.
您是否尝试过 java -cp guava.jar ... ?
Have you tried
java -cp guava.jar ...
?当您从 Eclipse 运行程序时,要在控制台上尽可能精确地运行程序,您需要从项目的根目录(而不是 bin)运行它,并且不要忘记提及类路径(http://download.oracle.com/docs /cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/classpath.html)
因此,例如在 root 上您将运行:
To run the program on console as precise as possible with when you run it from Eclipse, you need to run it from the root directory of the project (not from bin) and don't forget to mention the classpath (http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/classpath.html)
So for example on root you will run: