如何将我的Java程序转换为.EXE文件?

发布于 2025-01-23 13:05:40 字数 81 浏览 4 评论 0 原文

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

我还需要一个安装程序的程序。

If I have a Java source file (*.java) or a class file (*.class), how can I convert it to a .exe file?

I also need an installer for my program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

看透却不说透 2025-01-30 13:05:40

javapackager

Java Packager工具编译,软件包和准备Java和Javafx应用程序以进行分发。 javapackager命令是命令行版本。

- Oracle的文档

Javapackager 用JDK船舶运输。它可以使用 nativate exe flag生成.exe文件。

winrun4j

winrun4j是Windows的Java发射器。它是javaw.exe的替代方法,并提供以下好处:

  • 使用一个INI文件来指定类Path,主类,VM ARGS,程序Args。
  • 任务管理器中出现的自定义可执行名称。
  • 附加的JVM args用于更灵活的内存使用。
  • 自定义图标的内置图标替代器。
  • [更多的子弹点,请关注]

- winrun4j的网页

winrun4j是开源实用程序。它具有许多功能。

packr

包装您的JAR,资产和JVM,用于在Windows,Linux和Mac OS X上分发,并添加本机可执行文件以使其看起来像本机应用程序。 Packr最适合GUI应用。

- Packr readme

Packr是另一个开源工具。

jsmooth

JSmooth是Java可执行包装器。它为您的Java应用程序创建本机Windows Launcher(标准.EXE)。它使Java部署变得更加顺畅,用户友好,因为它可以单独找到任何已安装的Java VM。

- JSmooth的网站

JSmooth是开源的,具有功能,但它很旧。最后一个版本是在2007年。

jexepack

jexepack 是命令行工具(非常适合自动脚本),可选地包装Java应用程序(类文件),可选地及其资源(例如GIF/JPG/TXT/等) ,进入单个压缩 32位Windows EXE,该Windows Exe使用Sun的Java运行时环境运行。支持控制台和窗户应用程序。

- Jexepack的网站

Jexepack是试用器。生产使用所需的付款,并且使用此工具创建的EXE文件将显示“提醒”而无需付款。此外,最后一个版本是2013年。

installanywhere

installanywhere使开发人员可以轻松为任何平台创建专业的安装软件。借助Senternanywhere,您将迅速适应行业变化,更快地推向市场并提供引人入胜的客户体验。并在发货前了解项目OSS组件的脆弱性。

- 安装Whather的网站

installanywhere是一个商业/企业软件包,为基于Java的程序生成安装程序。它可能能够创建.exe文件。

可执行JAR文件

作为.exe文件的替代方案,您可以创建一个JAR文件,双击时自动运行,


有关更多信息,

有关此主题的绝佳信息来源是本文,“ 将java转换为exe - 为什么,何时,何时不及时,”,dmitry leskov,dmitry leskov,

另请参见伴侣页面”

javapackager

The Java Packager tool compiles, packages, and prepares Java and JavaFX applications for distribution. The javapackager command is the command-line version.

– Oracle's documentation

The javapackager utility ships with the JDK. It can generate .exe files with the -native exe flag, among many other things.

WinRun4J

WinRun4j is a java launcher for windows. It is an alternative to javaw.exe and provides the following benefits:

  • Uses an INI file for specifying classpath, main class, vm args, program args.
  • Custom executable name that appears in task manager.
  • Additional JVM args for more flexible memory use.
  • Built-in icon replacer for custom icon.
  • [more bullet points follow]

– WinRun4J's webpage

WinRun4J is an open source utility. It has many features.

packr

Packages your JAR, assets and a JVM for distribution on Windows, Linux and Mac OS X, adding a native executable file to make it appear like a native app. Packr is most suitable for GUI applications.

– packr README

packr is another open source tool.

JSmooth

JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself.

– JSmooth's website

JSmooth is open source and has features, but it is very old. The last release was in 2007.

JexePack

JexePack is a command line tool (great for automated scripting) that allows you to package your Java application (class files), optionally along with its resources (like GIF/JPG/TXT/etc), into a single compressed 32-bit Windows EXE, which runs using Sun's Java Runtime Environment. Both console and windowed applications are supported.

– JexePack's website

JexePack is trialware. Payment is required for production use, and exe files created with this tool will display "reminders" without payment. Also, the last release was in 2013.

InstallAnywhere

InstallAnywhere makes it easy for developers to create professional installation software for any platform. With InstallAnywhere, you’ll adapt to industry changes quickly, get to market faster and deliver an engaging customer experience. And know the vulnerability of your project’s OSS components before you ship.

– InstallAnywhere's website

InstallAnywhere is a commercial/enterprise package that generates installers for Java-based programs. It's probably capable of creating .exe files.

Executable JAR files

As an alternative to .exe files, you can create a JAR file that automatically runs when double-clicked, by adding an entry point to the JAR manifest.


For more information

An excellent source of information on this topic is this article, "Convert Java to EXE – Why, When, When Not and How", by Dmitry Leskov

See also the companion page "Best JAR to EXE Conversion Tools, Free and Commercial"

山川志 2025-01-30 13:05:40

lainish4j

启动4J是一种跨平台工具,用于包装Java应用程序在轻质Windows本机可执行文件中分发为JARS。可以将可执行文件配置为搜索某个JRE版本或使用捆绑的版本,并且可以设置运行时选项,例如初始/最大堆大小。包装器还可以通过应用程序图标,本机前启动屏幕,自定义过程名称和Java下载页面提供更好的用户体验,以防适当的JRE找到。

- 启动4J的网站

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

春花秋月 2025-01-30 13:05:40

更新: GCJ已死。它是正式删除了2016年的GCC项目。甚至在此之前,它实际上被放弃了七年,无论如何,它从来没有足够完成作为可行的替代Java实现。

去找另一个Java AOT编译器。

gcj:将Java源代码编译到本机机器代码中,包括Windows可执行文件。

虽然并非Java中的所有内容都得到了GCJ的支持,尤其是GUI组件(请参阅
常见问题)。我没有使用GCJ太多,但是从我对控制台应用程序进行的有限测试中,这似乎还不错。

使用GCJ创建独立可执行文件的一个缺点是,所得EXE的大小可能很大。一次,我在GCJ中编制了一个琐碎的控制台应用程序,结果是可执行文件约为1 MB。 (可能有一些我不知道的方法。另一个选择是可执行的压缩程序。)

就开源安装程序而言, nullsoft脚本安装系统是一个脚本安装程序。如果您很好奇,会有用户贡献了示例如果未安装所需的JRE,则会自动安装它。 (只是让您知道,我以前从未使用过NSI。)

有关使用NSIS安装Java应用程序的更多信息,请查看我对问题的回答分发Java应用程序的最佳方法是什么?

UPDATE: GCJ is dead. It was officially removed from the GCC project in 2016. Even before that, it was practically abandoned for seven years, and in any case it was never sufficiently complete to serve as a viable alternative Java implementation.

Go find another Java AOT compiler.

GCJ: The GNU Compiler for Java can compile Java source code into native machine code, including Windows executables.

Although not everything in Java is supported under GCJ, especially the GUI components (see
What Java API's are supported? How complete is the support? question from the FAQ). I haven't used GCJ much, but from the limited testing I've done with console applications, it seems fine.

One downside of using GCJ to create an standalone executable is that the size of the resulting EXE can be quite large. One time I compiled a trivial console application in GCJ and the result was an executable about 1 MB. (There may be ways around this that I am not aware of. Another option would be executable compression programs.)

In terms of open-source installers, the Nullsoft Scriptable Install System is a scriptable installer. If you're curious, there are user contributed examples on how to detect the presence of a JRE and install it automatically if the required JRE is not installed. (Just to let you know, I haven't used NSIS before.)

For more information on using NSIS for installing Java applications, please take a look at my response for the question "What's the best way to distribute Java applications?"

夜无邪 2025-01-30 13:05:40

您可以使用以下代码制作批处理文件:

start javaw -jar JarFile.jar

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

You could make a batch file with the following code:

start javaw -jar JarFile.jar

and convert the .bat to an .exe using any .bat to .exe converter.

伴随着你 2025-01-30 13:05:40

我们正在使用 install4j 以构建用于Windows或Unix环境的安装程序。

它很容易自定义,直到您想编写脚本以进行标准对话无法完成的特殊操作。但是,即使我们正在使用它来设置Windows服务,我们也只使用标准组件。

  • 安装程序 +启动器
  • Windows或Unix
  • 在Java
  • Ant任务
  • 中可脚本脚本可供自定义的标准面板和操作
  • 包括或下载JRE
  • 还可以启动Windows Services
  • 多种语言,

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

We're using Install4J to build installers for Windows or Unix environments.

It's easily customizable up to the point where you want to write scripts for special actions that cannot be done with standard dialogues. But even though we're setting up Windows services with it, we're only using standard components.

  • installer + launcher
  • Windows or Unix
  • scriptable in Java
  • ant task
  • lots of customizable standard panels and actions
  • optionally includes or downloads a JRE
  • can also launch Windows services
  • multiple languages

I think Launch4J is from the same company (just the launcher - no installer).

不喜欢何必死缠烂打 2025-01-30 13:05:40

最新版本的 java-web start 已经增强了以允许良好的离线操作以及允许“本地安装”。值得研究。

但是,截至2018年,Java Web的开始不再与最新的JDK捆绑在一起。 Oracle朝着“用封闭的JRE”模型推向“本地部署您的应用程序”。

The latest version of has been enhanced to allow good offline operation as well as allowing "local installation". It is worth looking into.

However, as of 2018 Java Web Start is no longer bundled with the newest JDKs. Oracle is pushing toward a "deploy your app locally with an enclosed JRE" model instead.

南薇 2025-01-30 13:05:40

从jdk14开始, jpackage @Jay答案中提到的Javapackager 。 Windows版本需要Wix 3.0,使用Java应用程序并构建提供EXE启动器的安装程序非常简单。

它也可以与 jlink 步骤也将通过jpackage隐式运行,但是我更喜欢单独制作JRE映像,因为只有在您更新JDK或在项目中添加新的模块依赖项时才会更改。

Java类的示例主要:

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生成安装程序,并使用app 62819060>版本- 安装每个重新安装)并打印所有系统属性 - 测试后删除参数“ -xshowsettings:属性:属性”:

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

As of JDK14, jpackage replaces javapackager mentioned in @Jay answer. The Windows version requires Wix 3.0 and it is fairly straightforward to take a java application and build an installer which provides EXE launcher.

It can also be used with jlink to build a cut-down Java runtime image which is bundled with the installer and only contains the set of modules needed to support your application. The jlink step will also be run implicitly by jpackage if no runtime is specified, but I prefer to make the JRE image separately as it will only change when you update JDK or add new module dependencies to your project.

Example main for Java class:

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]);
        }
    }
}

Here are example steps to build on Windows - obviously you'd set up your local build environment (Maven / ant / etc) to re-produce this:

mkdir jpackage.input\jars tmp
javac -d tmp src\exe\Main.java
pushd tmp && jar cvf ..\jpackage.input\jars\myapp.jar . && popd

Check it runs:

java -cp jpackage.input\jars\myapp.jar exe.Main X Y Z

Create a runtime image with jlink for the set of modules use by your application:

set jlink.modules=java.base
jlink --add-modules %jlink.modules% --strip-debug --no-man-pages --no-header-files --compress=1 --output jpackage.jre

In case there are missing modules above, you should check the jlink JRE runtime image can run your app:

jpackage.jre\bin\java -cp jpackage.input\jars\myapp.jar exe.Main X Y Z

Use jpackage to generate installer, with app version based on date+hour (this saves on need to un-install every re-install) and to print out all system properties - remove the parameter "-XshowSettings:properties" after testing:

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

Run the installer:

jpackage.dest\MyApp-%appver%.exe

Test the application:

"C:\Program Files\MyApp\MyApp.exe" ONE 2 THREE

... Prints system properties ...

args[0]=ONE
args[1]=2
args[2]=THREE
花之痕靓丽 2025-01-30 13:05:40

Imho jsmooth 似乎做得很好。

IMHO JSmooth seems to do a pretty good job.

怎会甘心 2025-01-30 13:05:40

我要说的是启动4J是将Java源代码(.java)转换为.exe文件的最佳工具
您甚至可以将JRE捆绑在一起以进行分发,甚至可以将EXE塑造出来。
尽管应用程序的大小增加,但即使用户未安装JRE,该应用程序也可以完美地工作。它还可以确保您能够为应用程序提供所需的特定JRE,而无需用户单独安装它。
但不幸的是,爪哇失去了其重要性。它的多平台支持被完全忽略,最终应用仅支持Windows。但这没什么大不了的,如果您仅适合Windows用户。

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.

我的黑色迷你裙 2025-01-30 13:05:40

您可以使用 janel 。这最后是应用程序启动器或服务启动器(可从4.x获得)。

You can use Janel. This last works as an application launcher or service launcher (available from 4.x).

旧故 2025-01-30 13:05:40

另外,您可以使用一些Java-to-C转换器(例如, jcgo )并编译生成的C将目标平台的本机二进制文件(.EXE)文件文件。

Alternatively, you can use some java-to-c translator (e.g., JCGO) and compile the generated C files to a native binary (.exe) file for the target platform.

风吹短裙飘 2025-01-30 13:05:40

可以原谅我不反对将Java程序转换为.EXE应用程序,而我有理由。主要的是,可以将Java程序从许多IDE中汇编成Jar文件。当程序以.jar格式为.JAR时,它可以在多个平台中运行,而不是在非常有限的环境中运行的.EXE。我的想法是,Java程序shoudl不会转换为EXE,除非它非常必要。人们总是可以写入.BAT文件,该文件是JAVA程序时运行JAVA程序的文件。

如果将其转换为EXE确实是必要的,JAR2EXE转换器会默默地执行此操作,并且还可以将库与主应用程序一起编译在一起。

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.

if it is really neccesary to convert it to exe, Jar2Exe converter silently does that and one can also attach Libraries that are compiled together with the Main Application.

烟酉 2025-01-30 13:05:40

您可以使用 jar2exe 将JAR转换为EXE。但是,您需要购买该软件。如果您需要开源软件,我建议 jsmooth

You can convert jar to exe using jar2exe. However you need to purchase the software. If you need a open source software i would suggest JSmooth.

只是一片海 2025-01-30 13:05:40

graalvm

这对我来说是一个足够好的软件,我早些时候已经使用过。
该软件可以将Java项目编译为本机可执行文件,也可以将JAR文件转换为本机可执行文件,这比我在网上找到的任何转换器要好得多,因为它可以更快,更顺畅。

graalvm是具有提前本机图像汇编的高级JDK。

您可以从类文件,jar文件或模块构建本机可执行文件。

GraalVM

This is a good enough software for me, i have used it earlier.
This software can compile java projects to native executables, it can also convert jar files to native executables, this is much better than any converter i found online as it works faster and smoother.

GraalVM is an advanced JDK with ahead-of-time Native Image compilation.

You can build a native executable from a class file, from a JAR file, or from a module.

じее 2025-01-30 13:05:40

谨慎说明:自这个问题首次提出以来,随着包装和部署的变化发生了很多变化。给出的大多数答案甚至都没有拼图(Java 9+)。

如果目标是创建特定的软件包,则在Oracle Docs Java 17 包装工具用户指南。本指南包括 jpackage 工具的文档,该工具允许人们为Linux,MacOS和Windows创建特定于平台的软件包。我认为特定于Windows的说明应包括到达.EXE文件,因为这仍然是Windows用户安装和运行应用程序的最熟悉的方式。

我自己的个人经验创建EXE(在iTch.io上出售)是在 Java平台,标准版Deployment Guide 中,其中包括使用工具Inno设置5 。该文档较早,适用于Java 9。 #jsdpg600“ rel =“ nofollow noreferrer”>在这里作为第一步,我使用 jlink 来制作一个独立的软件包。在我第一次争吵时,我无法弄清楚如何获得 jpackage 与我的模块化程序一起工作。但是,既然拼图已经存在了几年,那么 jpackage 现在可能更容易使用,这将是我可能为Windows用户发布的下一个Java应用程序的首选。

Cautionary note: Much has changed with packaging and deployment since this question was first asked. Most of the answers given are not even current with JIGSAW (Java 9+).

If the goal is to create OS specific packages, information is provided in Oracle Docs Java 17 Packaging Tool User Guide. This guide includes documentation for the jpackage tool, which allows one to create platform-specific packages for Linux, macOS and Windows. I assume the Windows-specific instructions should include arriving at an .exe file, since that remains the most familiar way for Windows users to install and run applications.

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.

随梦而飞# 2025-01-30 13:05:40

Java项目被导出为JAR可执行文件。当您想做一个Java项目的.EXE文件时,您可以做的是将罐子“转换”到exe(我指出我在引号转换之间放置了converts convert,因为这不是完全如此)。

从Intelij,您将只能生成罐子,

然后按照下一个示例尝试: https://www.genuinecoder.com/convert-java-jar-jar-to-exe/

Java projects are exported as Jar executables. When you wanna do a .exe file of a java project, what you can do is 'convert' the JAR to EXE (i remark that i putted between quotes convert because isn't exactly this).

From intelij you gonna be able to generate only the jar

Try following the next example : https://www.genuinecoder.com/convert-java-jar-to-exe/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文