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

我还需要一个安装程序。


当前回答

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

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

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

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

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

其他回答

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

I can be forgiven for being against converting a java program to a .exe Application and I have My reasons. the Major one being that a java program can be compiled to a jar file from A lot of IDE's. When the program is in .jar format, it can run in Multiple Platforms as opposed to .exe which would run Only in very limited Environment. I am for the Idea that Java Programs shoudl not be converted to Exe unless it is very neccesary. One can always write .bat files that runs the Java program while it is a jar file.

如果真的有必要将其转换为exe, Jar2Exe转换器可以默默地完成,也可以附加与主应用程序一起编译的库。

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

你可以用下面的代码创建一个批处理文件:

start javaw -jar JarFile.jar

并使用任何。bat到。exe转换器将。bat转换为。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.