运行jar依赖的java类文件

发布于 2025-01-04 04:06:50 字数 1095 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2025-01-11 04:06:50

确保当前目录也在类路径中。尝试运行:

java -cp .:jar1.jar:jar2.jar myprog

如果编译的类文件 myprog.class 位于当前目录中。

(这假设您的程序不在包中)。

Make sure that the current directory is also in the classpath. Try running with:

java -cp .:jar1.jar:jar2.jar myprog

if your compiled class file myprog.class is in the current directory.

(This assumes that your program is not in a package).

扭转时空 2025-01-11 04:06:50

您需要添加已编译的java程序(*.class)文件所在的目录。如果它位于当前目录中,那么您可以像这样运行它(注意“.”,它表示当前工作目录):

java -cp .:jar1.jar:jar2.jar myprog

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):

java -cp .:jar1.jar:jar2.jar myprog
留一抹残留的笑 2025-01-11 04:06:50

查看错误,它抱怨无法找到“userapps.SelectionTask_classes.SelectionTask”,这暗示类路径中的最后一个条目未正确指定。现在,假设您指定的 JAR 文件位于以下目录结构中:

./hadoop-0.20.1-core.jar

./lib/hadoopdb.jar

./userapps/SelectionTask_classes/

您将运行:

java -cp hadoop-0.20.1-core.jar:lib/hadoopdb.jar:userapps/SelectionTask_classes/ myprog

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:

java -cp hadoop-0.20.1-core.jar:lib/hadoopdb.jar:userapps/SelectionTask_classes/ myprog
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文