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

我还需要一个安装程序。


当前回答

Java项目被导出为Jar可执行文件。当你想做一个java项目的。EXE文件,你可以做的是“转换”JAR到EXE(我说,我把引号之间转换,因为不是完全这样)。

从intelij你将只能生成罐子

尝试下面的例子:https://www.genuinecoder.com/convert-java-jar-to-exe/

其他回答

从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.

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

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

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

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