运行jar依赖的java类文件
java.lang.NoClassDefFoundError
我有一个依赖于两个 jar 文件的 java 程序。 我使用命令编译程序:
javac -classpath jar1.jar:jar2.jar myprog.java
它编译成功。
但是当我尝试使用命令运行程序时: java -cp jar1.jar:jar2.jar myprog
,它抛出 java.lang.NoClassDefFoundError
。请帮忙,我哪里错了?
我正在使用 ubuntu 10.04。
实际错误:
Exception in thread "main" java.lang.NoClassDefFoundError: userapps/SelectionTask_classes/SelectionTask
Caused by: java.lang.ClassNotFoundException: userapps.SelectionTask_classes.SelectionTask
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: userapps/SelectionTask_classes/SelectionTask. Program will exit.
其中SelectionTask是我成功编译后生成的类文件。
java.lang.NoClassDefFoundError
I have a java program which is dependant on two jar files.
i compile the program using command :
javac -classpath jar1.jar:jar2.jar myprog.java
and it compiles successfully.
But when i try to run the program using command : java -cp jar1.jar:jar2.jar myprog
, it is throwing the java.lang.NoClassDefFoundError
. please help , where am i wrong ?
i am using ubuntu 10.04.
Actual error :
Exception in thread "main" java.lang.NoClassDefFoundError: userapps/SelectionTask_classes/SelectionTask
Caused by: java.lang.ClassNotFoundException: userapps.SelectionTask_classes.SelectionTask
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: userapps/SelectionTask_classes/SelectionTask. Program will exit.
where SelectionTask is my class file generated after successful compilation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保当前目录也在类路径中。尝试运行:
如果编译的类文件
myprog.class
位于当前目录中。(这假设您的程序不在包中)。
Make sure that the current directory is also in the classpath. Try running with:
if your compiled class file
myprog.class
is in the current directory.(This assumes that your program is not in a package).
您需要添加已编译的java程序(*.class)文件所在的目录。如果它位于当前目录中,那么您可以像这样运行它(注意“.”,它表示当前工作目录):
You need to add directory where your compiled java program (*.class) file is located. If it's in current directory then you could run it like this (notice "." which denotes current working directory):
查看错误,它抱怨无法找到“userapps.SelectionTask_classes.SelectionTask”,这暗示类路径中的最后一个条目未正确指定。现在,假设您指定的 JAR 文件位于以下目录结构中:
./hadoop-0.20.1-core.jar
./lib/hadoopdb.jar
./userapps/SelectionTask_classes/
您将运行:
Look at the error, it's complaining about not being able to find "userapps.SelectionTask_classes.SelectionTask", which hints at the last entry in your classpath not beign specified correctly. Now, assuming that the JAR files you have specified are in the following directory structure:
./hadoop-0.20.1-core.jar
./lib/hadoopdb.jar
./userapps/SelectionTask_classes/
You would run: