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

我还需要一个安装程序。


当前回答

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

其他回答

注意:自第一次提出这个问题以来,打包和部署已经发生了很大变化。给出的大多数答案甚至都不是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.

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

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

I would say launch4j is the best tool for converting a java source code(.java) to .exe file You can even bundle a jre with it for distribution and the exe can even be iconified. Although the size of application increases, it makes sure that the application will work perfectly even if the user does not have a jre installed. It also makes sure that you are able to provide the specific jre required for your app without the user having to install it separately. But unfortunately, java loses its importance. Its multi platform support is totally ignored and the final app is only supported for windows. But that is not a big deal, if you are catering only to windows users.

从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

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

start javaw -jar JarFile.jar

并使用任何。bat到。exe转换器将。bat转换为。exe。