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

我还需要一个安装程序。


当前回答

最新的Java Web Start已得到增强,以支持良好的脱机操作以及“本地安装”。这是值得研究的。

EDIT 2018: Java Web Start不再与最新的JDK绑定。甲骨文正在推动一种“使用封闭的JRE在本地部署应用程序”的模式。

其他回答

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

最新的Java Web Start已得到增强,以支持良好的脱机操作以及“本地安装”。这是值得研究的。

EDIT 2018: Java Web Start不再与最新的JDK绑定。甲骨文正在推动一种“使用封闭的JRE在本地部署应用程序”的模式。

您可以使用jar2exe将jar转换为exe。但是你需要购买软件。如果你需要一个开源软件,我会建议JSmooth。

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