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

我还需要一个安装程序。


当前回答

更新:GCJ死了。它于2016年正式从海湾合作委员会项目中移除。甚至在此之前,它实际上已经被废弃了7年之久,而且无论如何,它都不够完整,无法作为可行的Java实现替代方案。 去找另一个Java AOT编译器。

GCJ: GNU Java编译器可以将Java源代码编译成本机机器码,包括Windows可执行文件。

虽然GCJ并不支持Java中的所有内容,特别是GUI组件(请参阅GCJ 支持哪些Java API ?支持有多完整?FAQ中的问题)。我很少使用GCJ,但从我对控制台应用程序所做的有限测试来看,它似乎还不错。

使用GCJ创建独立可执行文件的一个缺点是,生成的EXE的大小可能相当大。有一次我在GCJ中编译了一个简单的控制台应用程序,结果是一个大约1 MB的可执行文件(可能有我不知道的方法)。另一个选择是可执行的压缩程序。)

在开源安装程序方面,Nullsoft Scriptable Install System是一个可脚本的安装程序。如果您感到好奇,可以参考用户提供的示例,了解如何检测JRE的存在并在未安装所需JRE时自动安装JRE。(只是想让你知道,我以前没有用过NSIS。)

有关使用NSIS安装Java应用程序的更多信息,请参阅我对“分发Java应用程序的最佳方式是什么?”这个问题的回答。

其他回答

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

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

从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

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.

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