java 如何使用 netbeans 创建可执行的 .jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1946394/
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
How to create executable .jar file with netbeans
提问by landscape
I'd like to make "double-click" cli application but still don't get how. I know I should propably somehow edit manifest but that is all. I googled ofc. but no success. Thanks for any tips. Here is the output from build, run, and manifest:
我想制作“双击”cli 应用程序,但仍然不知道如何制作。我知道我应该以某种方式编辑清单,但仅此而已。我用谷歌搜索了ofc。但没有成功。感谢您提供任何提示。以下是构建、运行和清单的输出:
compile:
Created dir: /home/nick/NetBeansProjects/SemestralWork/dist
Building jar: /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "/home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar"
jar:
BUILD SUCCESSFUL (total time: 1 second)
java -jar /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
at java.net.URLClassLoader.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:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: semestralwork.Main. Program will exit.
MY MANIFEST created with build:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.0-b08 (Sun Microsystems Inc.)
Main-Class: semestralwork.Main
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
回答by bguiz
These two lines tell you all you need to know:
这两行告诉你所有你需要知道的:
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
And a further clue is dropped by the manifest output:
清单输出删除了进一步的线索:
Main-Class: semestralwork.Main
This means that the JAR file is looking for a package named semestralworkand a class named Maininside it. It fails at this point because it cannot find either the semestralworkpackage or the Mainclass.
这意味着 JAR 文件正在寻找一个名为包semestralwork和Main在其中命名的类。此时它失败了,因为它找不到semestralwork包或Main类。
As you pointed out in your question, the problem is indeed in the manifest file. You can edit this directly in your JAR file if you like, but a better idea would be to do this from Netbeans:
正如您在问题中指出的那样,问题确实出在清单文件中。如果愿意,您可以直接在 JAR 文件中编辑它,但更好的主意是从 Netbeans 执行此操作:
- Click on `File --> Project Properties (semestralwork)'
- In the dialog that opens, on the tree on the left select
Run - Then, on the right, under the field labeled
Main class:, enter the fully qualified class nameof the class that you want executed when run from the command line.
- 单击“文件 --> 项目属性(semestralwork)”
- 在打开的对话框中,在左侧的树上选择
Run - 然后,在右侧标记为 的字段下
Main class:,输入从命令行运行时要执行的类的完全限定类名称。
In your case, as I see from your comment on @Aaron's answer, if your main class is in a file called encryption.java, and it is in the default package (no package), just enter encryption.
在你的情况下,正如我从你对@Aaron 的回答的评论中看到的,如果你的主类在一个名为 的文件中encryption.java,并且它在默认包(无包)中,只需输入encryption.
Once this is done, do a clean and build, then try running it from the command line again.
完成后,进行清理和构建,然后再次尝试从命令行运行它。
HTH
HTH
回答by Mare
Since I encountered the same problem, I can clarify the solution a little bit.
由于我遇到了同样的问题,我可以稍微澄清一下解决方案。
You must create main Java class outside your method (for example-default_package folder), and then invoke your method(folder), e.g import your_folder.connected_class;in that main class.
您必须在您的方法之外创建主 Java 类(例如-default_package 文件夹),然后调用您的方法(文件夹),例如import your_folder.connected_class;在该主类中。
Hopefully I could help someone with the same problem.
希望我可以帮助有同样问题的人。
回答by user49740
It's easier to make a .exe from a .jar withoutnetbeans. Here are my suggestions: 1. Use a special application for this(ex: JSmooth, JEXECreator etc) 2. Make a C++ program that starts a JVM (see this tutorial)
在没有netbeans 的情况下从 .jar 制作 .exe 更容易。以下是我的建议: 1. 为此使用一个特殊的应用程序(例如:JSmooth、JEXECreator 等) 2. 制作一个启动 JVM 的 C++ 程序(请参阅本教程)
回答by user698816
It's Easy. Download a copy of netbeans. Make a new project in netbeans. Goto your main class in the Projects Explorer. This should be folder "YourProject" As the class YOURPROJECT.JAVA . It is this yourproject.java file that you want to start with. Just write your code into the public static void main area. You can run the program with the green play button in the top toolbar.
这简单。下载一份 netbeans。在 netbeans 中创建一个新项目。在 Projects Explorer 中转到您的主类。这应该是文件夹“YourProject”作为类 YOURPROJECT.JAVA 。您要从这个 yourproject.java 文件开始。只需将您的代码写入 public static void main 区域。您可以使用顶部工具栏中的绿色播放按钮运行该程序。
回答by whatnick
The default class search path may be the issue. You should try changing directory to the location the jar is and launching with java -jar Semestral.jar. Also you may have misnamed the main class. Please also include your package structure.
默认的类搜索路径可能是问题所在。您应该尝试将目录更改为 jar 所在的位置并使用 java -jar Semestral.jar 启动。此外,您可能错误地命名了主类。还请包括您的包结构。
回答by Aaron Digulla
Open the JAR file with a ZIP tool (or try less ...if you're on Linux or jar tvf ...). Make sure there is a directory semestralworkin there which contains a file Main.class.
使用 ZIP 工具打开 JAR 文件(less ...如果您使用的是 Linux 或 ,请尝试jar tvf ...)。确保那里有一个semestralwork包含文件的目录Main.class。

