类路径编译正确,但无法运行。我缺少什么
你好,
我正在玩 mahout,我编写了一个基本的 java 类,它导入了一些图书馆的。编译时我的类路径似乎是正确的,我没有收到任何错误或投诉。
然而,当我运行编译后的类时,我收到一个异常:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
Greetings,
I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.
However when I run the compiled class I get an exception saying...
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的猜测是
.
不在您的类路径上。例如,您可能正在编译:但随后要运行您需要的代码
您正在编译的代码不需要位于类路径中,因为您正在提供代码(因此找不到任何内容) - 这就是它能够编译但无法运行的原因。
当然,这只是一个猜测 - 如果您可以发布用于编译和运行代码的命令,那将会有所帮助。
My guess is that
.
is not on your classpath. For example, you might be compiling with:but then to run the code you'd need
The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.
That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.
您的类路径中缺少
slf4j-api.jar
。使用 SLF4J,您始终需要slf4j-api.jar
和另一个 jar 绑定日志框架。实际上,如果您不关心日志记录,请使用slf4j-nop.jar
而不是slf4j-log12.jar
。更新: Mahout 似乎可以在 Maven 中央存储库中使用,因此使用 Maven 可以简化类路径设置过程。如果您不喜欢学习 Maven,请考虑使用 MOP,它是一个命令行启动器来运行 Java 东西它可以透明地下载 Maven 工件及其依赖项并设置您的类路径。
You're missing
slf4j-api.jar
on your class path. With SLF4J, you always needslf4j-api.jar
and another jar to bind a logging framework. And actually, if you don't care about logging, useslf4j-nop.jar
instead ofslf4j-log12.jar
.Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.
编译时类路径听起来不错;运行时类路径错误。
来自该类的 javadoc:
您在当前目录中看到 Test.class 文件吗?也许你错误地把它编译到了另一个路径。
Compile time classpath sounds right; runtime classpath is wrong.
From the javadocs for that class:
Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.
如果您使用 Mahout,请注意,使用 Maven 构建后,它会在 target/ 目录中生成“*.job”文件,其中包含打包在一起的所有依赖项。它只是一个 .jar 文件。
If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.