如何创建可执行的 Java 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/804466/
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 do I create executable Java program?
提问by
I have programmed a Java Program in JCreator, everything is done, but I want to create an executable file from it, ie I dont want to have to run the program by loading the java classes and compiling then executing, but instead have it as a stand alone executable file.
我已经在 JCreator 中编写了一个 Java 程序,一切都完成了,但我想从中创建一个可执行文件,即我不想通过加载 Java 类并编译然后执行来运行程序,而是将它作为独立的可执行文件。
What the quickest way to do this?
这样做的最快方法是什么?
回答by allyourcode
Write a script and make it executable. The script should look like what you'd normally use at the command line:
编写脚本并使其可执行。该脚本应类似于您通常在命令行中使用的内容:
java YourClass
This assumes you've already compiled your .java files and that the java can find your .class files. If java cannot find your .class files, you may want to look at using the -classpath option or setting your CLASSPATH environment variable.
这假设您已经编译了 .java 文件并且 java 可以找到您的 .class 文件。如果 java 找不到您的 .class 文件,您可能需要查看使用 -classpath 选项或设置 CLASSPATH 环境变量。
回答by ForYourOwnGood
On the command line, navigate to the root directory of the Java files you wish to make executable.
在命令行上,导航到您希望使其可执行的 Java 文件的根目录。
Use this command:
使用这个命令:
jar -cvf [name of jar file] [name of directory with Java files]
This will create a directory called META-INF in the jar archive. In this META-INF there is a file called MANIFEST.MF, open this file in a text editor and add the following line:
这将在 jar 存档中创建一个名为 META-INF 的目录。在这个 META-INF 中有一个名为 MANIFEST.MF 的文件,在文本编辑器中打开这个文件并添加以下行:
Main-Class: [fully qualified name of your main class]
then use this command:
然后使用这个命令:
java -jar [name of jar file]
and your program will run :)
你的程序将运行:)
回答by lothar
回答by danswain
I'm not quite sure what you mean.
我不太清楚你的意思。
But I assume you mean either 1 of 2 things.
但我假设你的意思是两件事之一。
- You want to create an executable .jar file
- 您想创建一个可执行的 .jar 文件
Eclipse can do this really easily File --> Export and create a jar and select the appropriate Main-Class and it'll generate the .jar for you. In windows you may have to associate .jar with the java runtime. aka Hold shift down, Right Click "open with" browse to your jvm and associate it with javaw.exe
Eclipse 可以非常轻松地完成此操作 File --> Export 并创建一个 jar 并选择适当的 Main-Class,它会为您生成 .jar。在 Windows 中,您可能必须将 .jar 与 java 运行时相关联。又名按住shift键,右键单击“打开方式”浏览到您的jvm并将其与javaw.exe关联
- create an actual .exe file then you need to use an extra library like
- 创建一个实际的 .exe 文件,然后你需要使用一个额外的库,比如
http://jsmooth.sourceforge.net/or http://launch4j.sourceforge.net/will create a native .exe stub with a nice icon that will essentially bootstrap your app. They even figure out if your customer hasn't got a JVM installed and prompt you to get one.
http://jsmooth.sourceforge.net/或http://launch4j.sourceforge.net/将创建一个带有漂亮图标的本机 .exe 存根,它基本上可以引导您的应用程序。他们甚至会判断您的客户是否没有安装 JVM 并提示您安装。
回答by Vini
As suggested earlier too, you can look at launch4j to create the executable for your JAR file. Also, there is something called "JExePack" that can put an .exe wrapper around your jar file so that you can redistribute it (note: the client would anyways need a JRE to run the program on his pc) Exes created with GCJ will not have this dependency but the process is a little more involved.
正如前面所建议的,您可以查看 launch4j 来为您的 JAR 文件创建可执行文件。此外,还有一种叫做“JExePack”的东西可以在你的 jar 文件周围放置一个 .exe 包装器,以便你可以重新分发它(注意:客户端无论如何都需要一个 JRE 来在他的电脑上运行程序)用 GCJ 创建的 Exes 不会有这种依赖性,但这个过程有点复杂。
回答by KitsuneYMG
Take a look at WinRun4J. It's windows only but that's because unix has executable scripts that look (to the user) like bins. You can also easily modify WinRun4J to compile on unix.
看看WinRun4J。它只是 Windows,但那是因为 unix 具有看起来(对用户)像 bins 的可执行脚本。您还可以轻松修改 WinRun4J 以在 unix 上进行编译。
It does require a config file, but again, recompile it with hard-coded options and it works like a config-less exe.
它确实需要一个配置文件,但同样,使用硬编码选项重新编译它,它就像一个无配置的 exe。
回答by OscarRyz
You can use the jar toolbundled with the SDK and create an executable version of the program.
您可以使用SDK 捆绑的jar 工具并创建程序的可执行版本。
This is how it's done.
这就是它的完成方式。
I'm posting the results from my command prompt because it's easier, but the same should apply when using JCreator.
我从我的命令提示符发布结果,因为它更容易,但在使用 JCreator 时应该同样适用。
First create your program:
首先创建你的程序:
$cat HelloWorldSwing.java
package start;
import javax.swing.*;
public class HelloWorldSwing {
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
class Dummy {
// just to have another thing to pack in the jar
}
Very simple, just displays a window with "Hello World"
很简单,只显示一个带有“Hello World”的窗口
Then compile it:
然后编译它:
$javac -d . HelloWorldSwing.java
Two files were created inside the "start" folder Dummy.classand HelloWorldSwing.class.
在“开始”文件夹Dummy.class和HelloWorldSwing.class中创建了两个文件。
$ls start/
Dummy.class HelloWorldSwing.class
Next step, create the jar file. Each jar file have a manifest file, where attributes related to the executable file are.
下一步,创建 jar 文件。每个 jar 文件都有一个清单文件,其中包含与可执行文件相关的属性。
This is the content of my manifest file.
这是我的清单文件的内容。
$cat manifest.mf
Main-class: start.HelloWorldSwing
Just describe what the main class is ( the one with the public static void main method )
只需描述主类是什么(具有 public static void main 方法的类)
Once the manifest is ready, the jar executable is invoked.
清单准备好后,将调用 jar 可执行文件。
It has many options, here I'm using -c -m -f ( -c to create jar, -m to specify the manifest file , -f = the file should be named.. ) and the folder I want to jar.
它有很多选项,这里我使用 -c -m -f(-c 创建 jar,-m 指定清单文件,-f = 文件应该命名为..)和我想要 jar 的文件夹。
$jar -cmf manifest.mf hello.jar start
This creates the .jar file on the system
这会在系统上创建 .jar 文件
You can later just double click on that file and it will run as expected.
您稍后只需双击该文件,它就会按预期运行。
To create the .jar file in JCreator you just have to use "Tools" menu, create jar, but I'm not sure how the manifest goes there.
要在 JCreator 中创建 .jar 文件,您只需要使用“工具”菜单,创建 jar,但我不确定清单是如何到达那里的。
Here's a video I've found about: Create a Jar File in Jcreator.
这是我找到的一个视频:在 Jcreator 中创建 Jar 文件。
I think you may proceed with the other links posted in this thread once you're familiar with this ".jar" approach.
我认为,一旦您熟悉了这种“.jar”方法,您就可以继续使用此线程中发布的其他链接。
You can also use jnlp( Java Network Launcher Protocol ) too.
您也可以使用jnlp(Java 网络启动器协议)。
回答by Andrew Thompson
Java Web Start is a good technology for installing Java rich clients off the internet direct to the end user's desktop (whether the OS is Windows, Mac or *nix). It comes complete with desktop integration and automatic updates, among many other goodies.
Java Web Start 是一种很好的技术,可以将 Java 富客户端从 Internet 直接安装到最终用户的桌面(无论操作系统是 Windows、Mac 还是 *nix)。它配备了桌面集成和自动更新,以及许多其他优点。
For more information on JWS, see the JWS info page.
有关 JWS 的更多信息,请参阅JWS 信息页面。
回答by Joe
Jexecutable can create Windows exe for Java programs. It embeds the jars into exe file and you can run it like a Windows program.
Jexecutable 可以为 Java 程序创建 Windows exe。它将 jars 嵌入到 exe 文件中,您可以像运行 Windows 程序一样运行它。