Java NoClassDefFoundError

发布于 2024-12-20 00:02:34 字数 832 浏览 0 评论 0原文

对于学校的作业,我需要创建一个类 Blender 来实现一些预定义的东西。我收到了一个 JAR 文件 imagecompositor.jar,它可以完成所有操作并使用 Blender 类。 JAR 文件包含两个类(ImageCompositor.classStDraw.class)。

要运行该程序,老师们说我必须运行以下内容:

java -cp ... ass3.ImageCompositor img1 img2 offsetx offsety

我只知道如何编写 Java 代码。我不知道如何将外部类加载到 JAR 文件中。所以我尝试了这个:

java -cp imagecompositor.jar ass3.ImageCompositor img1.png img2.png 0 0

但它抛出了 NoClassDefFoundError

Exception in thread "main" java.lang.NoClassDefFoundError: ass3/Blender
at ass3.ImageCompositor.main(ImageCompositor.java:113)

Blender 类中有行 package ass3 。我做错了什么?

我想提供更多信息,但我不知道如何获取堆栈跟踪等。

For an assignment at school, I need to make a class Blender that implements some pre-defined things. I received a JAR file, imagecompositor.jar, that does everything and makes use of the Blender class. The JAR file contains two classes (ImageCompositor.class and StDraw.class).

To run the program, the teachers say that I have to run the following:

java -cp ... ass3.ImageCompositor img1 img2 offsetx offsety

I only know how to code Java. I have no idea how to load external classes into a JAR file. So I tried this:

java -cp imagecompositor.jar ass3.ImageCompositor img1.png img2.png 0 0

but it threw a NoClassDefFoundError:

Exception in thread "main" java.lang.NoClassDefFoundError: ass3/Blender
at ass3.ImageCompositor.main(ImageCompositor.java:113)

The Blender class has the line package ass3 in it. What am I doing wrong?

I want to give more information, but I don't know how to get the stack trace, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

懒猫 2024-12-27 00:02:34

您需要将 Blender 类添加到类路径中。

命令行

java -cp imagecompositor.jar ...

使用 imagecompositor.jar 的类路径。这告诉 Java 虚拟机 (JVM) 查找 jar 文件。它没有告诉 JVM 去​​寻找你的类。如果您已将它们打包到 jar 文件中,则应该这样做

java -cp imagecompositor.jar;myclasses.jar ...

,并且如果 .class 文件位于根目录 path/to/someRoot 的树中(例如类 ass3.Blender code> 应该在 path/to/someRoot/ass3/Blender.class 中),那么您应该使用相同的语法,但使用根目录而不是 .jar 文件(我认为< /em> 这是对的,不能查找文档):

java -cp imagecompositor.jar;path/to/someRoot ...

You need to add your Blender class to the classpath.

The command line

java -cp imagecompositor.jar ...

uses a classpath of imagecompositor.jar. This tells the Java virtual machine (JVM) to look in the jar file. What it doesn't tell the JVM to do is to look for your classes. If you've packaged them into a jar file, you should do

java -cp imagecompositor.jar;myclasses.jar ...

and if the .class files are in a tree with root directory path/to/someRoot (e.g. class ass3.Blender should be in path/to/someRoot/ass3/Blender.class), then you should use the same syntax but with the root directory rather than a .jar file (I think this is right, can't find docs):

java -cp imagecompositor.jar;path/to/someRoot ...
垂暮老矣 2024-12-27 00:02:34

Blender 类位于哪里?为了正确找到,您的 Blender 类(在“ass”包中)必须位于相对于当前目录的名为“ass”的目录中。如果是(并且您没有从默认类路径中删除当前的director .),那么您不需要修改您的类路径。

但是,在您的情况下,您需要修改类路径(以便指定提供的 jar 文件),并且您的帖子中列出的命令确实删除。< /code> 从路径。因此,您必须执行以下操作:(

    java -cp whatever.jar;. whatever_other_parameters

请注意 -CP 参数后附加的“;.”。)这会将当前目录放回到类路径中。

Where is the Blender class located? To be found properly, your the Blender class (in package "ass") must be located in a directory named "ass" relative to your current directory. If it is (and you do not remove the current director . from the default class path) then you do not need to modify your class path.

However, in your case you need to modify the class path (in order to specify the supplied jar file), and command listed in your post does remove . from the path. So you must do the following:

    java -cp whatever.jar;. whatever_other_parameters

(Note the ";." appended to the -CP argument.) This puts the current directory back in the class path.

热血少△年 2024-12-27 00:02:34

java.lang.NoClassDefFoundError 通常是静态构造函数中的失败。如果 JVM 根本无法找到您的类,它会报告 java.lang.ClassNotFoundException。根据您的问题描述,我认为命令行是:

java -cp imagecompositor.jar ass3.ImageCompositor img1 img2 offsetx offsety

假设 imagecompositor.jar 文件与我们执行 java 命令的目录位于同一目录中。这告诉 java 编译器使 imagecompositor.jar 中的所有内容可供类加载器使用,并且我们正在加载一个名为 ImageCompositor 的类,该类位于 ass3 包中。我们还假设 ImageCompositor 有一个静态 main 方法,这就是 Java 从命令行运行内容的方式。

java.lang.NoClassDefFoundError is usually a failure in the static constructor. The JVM would say java.lang.ClassNotFoundException if it couldn't locate your class at all. From your question description, I'm thinking the commandline is:

java -cp imagecompositor.jar ass3.ImageCompositor img1 img2 offsetx offsety

which is assuming that the imagecompositor.jar file is in the same directory as where we are executing the java command. This is telling the java compiler to make everything in imagecompositor.jar available to the classloader and we are loading a class named ImageCompositor which resides in the ass3 package. We are also assuming that the ImageCompositor has a static main method, which is how Java runs stuff from the commandline.

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