如何创建可执行Java程序? [重复]

时间:2022-11-25 14:04:24

This question already has an answer here:

这个问题在这里已有答案:

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?

最快的方法是什么?

12 个解决方案

#1


57  

You can use the jar tool bundled 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.class and HelloWorldSwing.class.

在“start”文件夹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文件

如何创建可执行Java程序? [重复]

You can later just double click on that file and it will run as expected.

您可以稍后双击该文件,它将按预期运行。

如何创建可执行Java程序? [重复]

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 Network Launcher Protocol)。

#2


12  

If you are using Eclipse , you can try the below 7 steps to get a .exe file for Windows.

如果您使用的是Eclipse,则可以尝试以下7个步骤来获取Windows的.exe文件。

如何创建可执行Java程序? [重复]如何创建可执行Java程序? [重复]如何创建可执行Java程序? [重复]

Now you have a JAR file. Use java -jar path/jarname.jar to execute.

现在你有了一个JAR文件。使用java -jar path / jarname.jar来执行。

如何创建可执行Java程序? [重复]


If you want to convert this to .exe, you can try http://sourceforge.net/projects/launch4j/files/launch4j-3/

如果你想将它转换为.exe,你可以尝试http://sourceforge.net/projects/launch4j/files/launch4j-3/

如何创建可执行Java程序? [重复]如何创建可执行Java程序? [重复]

STEP7: Give the .xml file an appropriate name and click "Save". The .xml file is standard, don't worry about it. Your executable file will now be created!

STEP7:为.xml文件指定一个合适的名称,然后单击“保存”。 .xml文件是标准的,不用担心。您的可执行文件现在将被创建!

#3


11  

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可以非常轻松地执行此操作文件 - >导出并创建一个jar并选择适当的Main-Class,它将为您生成.jar。在Windows中,您可能必须将.jar与java运行时关联。 aka Hold shift down,右键单击“打开方式”浏览到您的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并提示您获取JVM。

#4


5  

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 :)

你的程序将运行:)

#5


3  

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有可执行脚本(对用户而言)就像垃圾箱一样。您还可以轻松修改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。

#6


3  

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。它将jar嵌入到exe文件中,你可以像Windows程序一样运行它。

#7


2  

You could use GCJ to compile your Java program into native code.
At some time they even compiled Eclipse into a native version.

您可以使用GCJ将Java程序编译为本机代码。有时他们甚至将Eclipse编译成本机版本。

#8


1  

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环境变量。

#9


1  

Take a look at launch4j

看看launch4j

#10


1  

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信息页面。

#11


0  

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不会有这种依赖,但过程涉及更多。

#12


0  

If you are using NetBeans, you just press f11 and your project will be build. The default path is projectfolder\dist

如果您使用的是NetBeans,只需按f11即可构建您的项目。默认路径是projectfolder \ dist

#1


57  

You can use the jar tool bundled 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.class and HelloWorldSwing.class.

在“start”文件夹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文件

如何创建可执行Java程序? [重复]

You can later just double click on that file and it will run as expected.

您可以稍后双击该文件,它将按预期运行。

如何创建可执行Java程序? [重复]

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 Network Launcher Protocol)。

#2


12  

If you are using Eclipse , you can try the below 7 steps to get a .exe file for Windows.

如果您使用的是Eclipse,则可以尝试以下7个步骤来获取Windows的.exe文件。

如何创建可执行Java程序? [重复]如何创建可执行Java程序? [重复]如何创建可执行Java程序? [重复]

Now you have a JAR file. Use java -jar path/jarname.jar to execute.

现在你有了一个JAR文件。使用java -jar path / jarname.jar来执行。

如何创建可执行Java程序? [重复]


If you want to convert this to .exe, you can try http://sourceforge.net/projects/launch4j/files/launch4j-3/

如果你想将它转换为.exe,你可以尝试http://sourceforge.net/projects/launch4j/files/launch4j-3/

如何创建可执行Java程序? [重复]如何创建可执行Java程序? [重复]

STEP7: Give the .xml file an appropriate name and click "Save". The .xml file is standard, don't worry about it. Your executable file will now be created!

STEP7:为.xml文件指定一个合适的名称,然后单击“保存”。 .xml文件是标准的,不用担心。您的可执行文件现在将被创建!

#3


11  

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可以非常轻松地执行此操作文件 - >导出并创建一个jar并选择适当的Main-Class,它将为您生成.jar。在Windows中,您可能必须将.jar与java运行时关联。 aka Hold shift down,右键单击“打开方式”浏览到您的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并提示您获取JVM。

#4


5  

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 :)

你的程序将运行:)

#5


3  

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有可执行脚本(对用户而言)就像垃圾箱一样。您还可以轻松修改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。

#6


3  

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。它将jar嵌入到exe文件中,你可以像Windows程序一样运行它。

#7


2  

You could use GCJ to compile your Java program into native code.
At some time they even compiled Eclipse into a native version.

您可以使用GCJ将Java程序编译为本机代码。有时他们甚至将Eclipse编译成本机版本。

#8


1  

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环境变量。

#9


1  

Take a look at launch4j

看看launch4j

#10


1  

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信息页面。

#11


0  

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不会有这种依赖,但过程涉及更多。

#12


0  

If you are using NetBeans, you just press f11 and your project will be build. The default path is projectfolder\dist

如果您使用的是NetBeans,只需按f11即可构建您的项目。默认路径是projectfolder \ dist