如果我有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.

其他回答

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

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

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-to-c转换器(例如JCGO),并将生成的C文件编译为目标平台的本机二进制(.exe)文件。

我们正在使用Install4J为windows或unix环境构建安装程序。

它很容易定制,直到您想要为标准对话框无法完成的特殊操作编写脚本。但是即使我们用它来设置windows服务,我们也只是使用标准组件。

安装程序+启动程序 Windows或Unix Java脚本 ant任务 大量可定制的标准面板和操作 可选地包括或下载JRE 还能启动Windows服务吗 多种语言

我认为Launch4J来自同一家公司(只有启动器-没有安装程序)。

PS:很遗憾我没有得到这个代言的报酬。我只是喜欢那个工具。

以我之见,JSmooth似乎做得相当不错。