如果我有Java源文件(*.java)或类文件(*.class),如何将其转换为.exe文件?

我还需要一个安装程序。


当前回答

注意:自第一次提出这个问题以来,打包和部署已经发生了很大变化。给出的大多数答案甚至都不是JIGSAW (Java 9+)的最新版本。

如果目标是创建特定于操作系统的包,Oracle Docs Java 17打包工具用户指南中提供了相关信息。本指南包括jpackage工具的文档,该工具允许为Linux、macOS和Windows创建特定于平台的包。我认为Windows特定的指令应该包括生成.exe文件,因为这仍然是Windows用户安装和运行应用程序最熟悉的方式。

My own personal experience creating an exe (for sale on itch.io) was with the Java Platform, Standard Edition Deployment Guide, which included making use of the tool Inno Setup 5. This documentation is older, is for Java 9. The section directly pertaining to .exe packaging is located here As a first step, I used jlink to make a self-contained package. At the time I was first wrangling with this, I was unable to figure out how to get jpackage to work with my modular program. But now that Jigsaw has been around for several years, jpackage is now likely much easier to use, and would be my first choice for the next Java app I might publish for Windows users.

其他回答

Launch4j

Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executables. The executable can be configured to search for a certain JRE version or use a bundled one, and it's possible to set runtime options, like the initial/max heap size. The wrapper also provides better user experience through an application icon, a native pre-JRE splash screen, a custom process name, and a Java download page in case the appropriate JRE cannot be found. – Launch4j's website

如果您需要将整个应用程序转换为本机代码,即EXE加dll,有ExcelsiorJET。我发现它工作得很好,并提供了捆绑JRE的替代方案。

编辑:这是在2010年发布的-该产品已不再可用。

从JDK14开始,jpackage取代了@Jay answer中提到的javapackager。Windows版本需要Wix 3.0,使用java应用程序构建一个提供EXE启动器的安装程序是相当简单的。

它还可以与jlink一起使用来构建精简的Java运行时映像,该映像与安装程序捆绑在一起,只包含支持应用程序所需的模块集。如果没有指定运行时,jlink步骤也将由jpackage隐式运行,但我更喜欢单独制作JRE映像,因为它只在更新JDK或向项目添加新的模块依赖项时才会更改。

Java类的main示例:

package exe;
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+i+"]="+args[i]);
        }
    }
}

下面是在Windows上构建的示例步骤——显然你需要设置你的本地构建环境(Maven / ant / etc)来重新生成:

mkdir jpackage.input\jars tmp
javac -d tmp src\exe\Main.java
pushd tmp && jar cvf ..\jpackage.input\jars\myapp.jar . && popd

检查它的运行情况:

java -cp jpackage.input\jars\myapp.jar exe.Main X Y Z

使用jlink为应用程序使用的模块集创建一个运行时映像:

set jlink.modules=java.base
jlink --add-modules %jlink.modules% --strip-debug --no-man-pages --no-header-files --compress=1 --output jpackage.jre

如果上面有缺失的模块,你应该检查jlink JRE运行时映像可以运行你的应用程序:

jpackage.jre\bin\java -cp jpackage.input\jars\myapp.jar exe.Main X Y Z

使用jpackage生成安装程序,基于日期+小时的应用程序版本(这节省了每次重新安装时卸载的需要),并打印出所有系统属性-测试后删除参数“- xshowsettings:properties”:

set appver=%date:~6,2%.%date:~3,2%.%date:~0,2%%time:~0,2%
jpackage --win-console --input jpackage.input --runtime-image jpackage.jre --app-version %appver% --type exe --name "MyApp" --dest jpackage.dest --java-options "-XshowSettings:properties" --main-jar jars\myapp.jar --main-class exe.Main

运行安装程序:

jpackage.dest\MyApp-%appver%.exe

测试应用程序:

"C:\Program Files\MyApp\MyApp.exe" ONE 2 THREE

... Prints system properties ...

args[0]=ONE
args[1]=2
args[2]=THREE

你可以用Janel。最后一个是作为应用程序启动器或服务启动器(可从4.x获得)。

或者,您可以使用一些java-to-c转换器(例如JCGO),并将生成的C文件编译为目标平台的本机二进制(.exe)文件。