Java - 找不到主类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3001497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Could not find the main class?
提问by asmo
I'm having a hard-time with a Java error. I'm getting this when double-clicking on my compiled project Jar file in order to run it:
我在遇到 Java 错误时遇到了困难。双击我编译的项目 Jar 文件以运行它时,我得到了这个:
Could not find the main class: testPackage.testFrame. Program will exit.
However, my main class is cleary defined in the project properties. Furthermore, everything works fine when I run the project directly from NetBeans.
但是,我的主类在项目属性中明确定义。此外,当我直接从 NetBeans 运行项目时,一切正常。
I'm using the ip2c.jar third-party library to determine countries from IP addresses. If I comment the class that is using the ip2c.jar file, I don't get the error.
我正在使用 ip2c.jar 第三方库根据 IP 地址确定国家/地区。如果我评论使用 ip2c.jar 文件的类,我不会收到错误消息。
How can a third-party library be preventing the JVM from finding my main class?
第三方库如何阻止 JVM 找到我的主类?
回答by Piotr
If you want to run your application by clicking on the jar you need to specify the main class in META-INF/MANIFEST.MFfile in the jar as it is described here. Make sure you have it properly defined.
如果你想通过点击罐子来运行你的应用程序,你需要指定主类META-INF/MANIFEST.MF,因为它描述的jar文件在这里。确保您已正确定义它。
You should be able to do this in Netbeans during creation of your jar file.
在创建 jar 文件期间,您应该能够在 Netbeans 中执行此操作。
You may also find this questionin SO helpful.
您可能还会发现这个问题很有帮助。
回答by ring bearer
If I comment the class that is using the ip2c.jar file, I don't get the error.
如果我评论使用 ip2c.jar 文件的类,我不会收到错误消息。
Is it that when you comment above mentioned usage, some classes get compiled properly? and when you include, you have some error, that causes the .class for your Main class not be generated? - just a hint as a third-party jar would not interfere with META-INF/MANIFEST.MF in main jar.
是不是当您评论上述用法时,某些类会被正确编译?当你包含时,你有一些错误,导致你的 Main 类的 .class 没有生成?- 只是一个提示,因为第三方 jar 不会干扰主 jar 中的 META-INF/MANIFEST.MF。
回答by kchmweno
I also had this problem. It happens that Java does not tell you the ERROR correctly. Whereas my Main class was correct, it told me that my Main class was not found. My error was that, I had not copied a new library to my lib folder outside of Netbeans. Hope you get this..I mean, I created a folder to run my JAR file, in this folder, I have a lib folder that I copied from my project. This may sound not very clear, If anyone has this error, I can help you out. Thanks.
我也有这个问题。碰巧 Java 没有正确地告诉您错误。虽然我的 Main 类是正确的,但它告诉我没有找到我的 Main 类。我的错误是,我没有将新库复制到 Netbeans 之外的 lib 文件夹中。希望你能明白。我的意思是,我创建了一个文件夹来运行我的 JAR 文件,在这个文件夹中,我有一个从我的项目中复制的 lib 文件夹。这听起来可能不是很清楚,如果有人遇到此错误,我可以帮助您。谢谢。
回答by dty
You need to ensure your manifest is correct and in the correct directory (which appears to be fine given then error message contains a class name), and that the class is in the JAR, and packaged with a correct directory structure which matches its package structure.
您需要确保您的清单正确且位于正确的目录中(如果错误消息包含类名,这似乎很好),并且该类位于 JAR 中,并使用与其包结构匹配的正确目录结构打包.

