简单 .jar 文件中的 NoClassDefFoundError
我正在尝试从我的项目中创建一个简单的 .jar 文件。该项目由两个 .class 文件组成 - 主类使用辅助类生成 GUI。主类是实际运行的“主”类,而第二个类只是一个带有其方法的类文件,它也是 JFrame 的扩展,并导入 javax.swing 和 java.awt.event。 *。
我使用 Jar 将其全部打包。我添加了一个清单文件(带有换行符),它使用 main 方法指向主文件。因此,Jar 文件有两个 .class 文件和一个包含 manifest.txt 的文件夹。当我使用 javaw.exe 运行它时,什么也没有发生。因此,我尝试在命令行中运行它,并收到有关辅助类的 NoClassDefFroundError 错误。
我注意到当我尝试在 JCreator 中编译和运行第二个类时,我遇到了同样的错误 - 难怪,它没有 main 方法,它只是一个类文件。当我从 JCreator 运行主文件时,一切正常。
有什么想法吗?
I'm trying to create a simple .jar file out of my project. The project is made of two .class files - the main class which uses the secondary class to generate a GUI. The main class is the actual "main" class that runs while the second class is just a class file with it's methods and it's also an extension of JFrame and imports javax.swing and java.awt.event.*.
I use Jar to bundle it all up. I add a manifest file (with a new line character) which points to the primary file with the main method. The Jar file thus has two .class files and a folder with the manifest.txt in it. When I use javaw.exe to run it, nothing happens at all. So I try to run it in the command line and I get a NoClassDefFroundError about the secondary class.
I noticed I get the same kind of error when I try to compile and run the second class in JCreator - no wonder, it doesn't have a main method, it's just a class file. When I run the main file from JCreator, everything works fine.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看您的堆栈跟踪,我现在可以看到问题:我可以告诉您实际上有两个以上的类:
有一个名为
grafPrime$calcButton.class
的文件,并且它需要位于 jar 文件中, 也。可能还有其他此类文件 - 请确保包含所有这些文件!Looking at your stack trace, I can now see the problem: I can tell you actually have more than two classes:
There's a file named
grafPrime$calcButton.class
, and it needs to be in the jar file, too. There may be other such files -- make sure you include all of them!好的,问题是您没有包含匿名类 - 您应该有一个名为
grafPrime$calcButton.class
的文件,但它不在您的 jar 文件中。基本上,将代码编译到一个干净的目录中,并包含生成的所有类文件。
Okay, the problem is that you've not included the anonymous class - you should have a file called
grafPrime$calcButton.class
, and that's not in your jar file.Basically, compile your code into a clean directory and include all the class files which are generated.