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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/147181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:55:07  来源:igfitidea点击:

How can I convert my Java program to an .exe file?

javainstallerexe

提问by Waseem

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

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

I also need an installer for my program.

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

采纳答案by Jay

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

Java Packager 工具编译、打包和准备用于分发的 Java 和 JavaFX 应用程序。javapackager 命令是命令行版本。

– Oracle 的文档

The javapackagerutility ships with the JDK. It can generate .exe files with the -native exeflag, among many other things.

javapackager实用程序随 JDK 一起提供。它可以生成带有-native exe标志的.exe 文件,等等。

WinRun4J

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 是一个用于 Windows 的 Java 启动器。它是 javaw.exe 的替代品,具有以下优点:

  • 使用 INI 文件来指定类路径、主类、vm args、程序 args。
  • 出现在任务管理器中的自定义可执行文件名称。
  • 额外的 JVM 参数以更灵活地使用内存。
  • 自定义图标的内置图标替换器。
  • [更多要点如下]

– WinRun4J 的网页

WinRun4J is an open source utility. It has manyfeatures.

WinRun4J 是一个开源实用程序。它有很多特点。

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

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

– 打包自述文件

packr is another open source tool.

packr 是另一个开源工具。

JSmooth

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 是一个 Java 可执行文件包装器。它为您的 Java 应用程序创建本机 Windows 启动器(标准 .exe)。它使 Java 部署更加顺畅和用户友好,因为它能够自己找到任何已安装的 Java VM。

– JSmooth 的网站

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

JSmooth 是开源的,有功能,但是很老了。最后一次发布是在 2007 年。

JexePack

JexePack

JexePackis 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 compressed32-bit Windows EXE, which runs using Sun's Java Runtime Environment. Both console and windowed applications are supported.

– JexePack's website

JexePack是一个命令行工具(非常适合自动化脚本编写),它允许您将 Java 应用程序(类文件)连同其资源(如 GIF/JPG/TXT/等)打包成单个压缩的32 位 Windows EXE ,它使用 Sun 的 Java 运行时环境运行。支持控制台和窗口应用程序。

– JexePack 的网站

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.

JexePack 是试用软件。生产使用需要付费,使用此工具创建的exe文件会在不付费的情况下显示“提醒”。此外,上一次发布是在 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 使开发人员可以轻松地为任何平台创建专业的安装软件。使用 InstallAnywhere,您将快速适应行业变化,更快地进入市场并提供引人入胜的客户体验。并在发货前了解项目 OSS 组件的漏洞。

– InstallAnywhere 的网站

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

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

Executable JAR files

可执行的 JAR 文件

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.

作为 .exe 文件的替代方法,您可以创建一个双击时自动运行的 JAR 文件,方法是向 JAR 清单中添加一个入口点



For more information

想要查询更多的信息

An excellent source of information on this topic is Excelsior's article "Convert Java to EXE – Why, When, When Not and How".

Excelsior 的文章“将 Java 转换为 EXE – 为什么、何时、何时不以及如何”是有关此主题的极好信息来源。

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

另请参阅配套文章“免费和商业的最佳 JAR 到 EXE 转换工具”。

回答by jussij

IMHO JSmoothseems to do a pretty good job.

恕我直言,JSmooth似乎做得很好。

回答by coobird

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

Go find another Java AOT compiler.

更新:GCJ 死了。它于 2016年正式从 GCC 项目中删除。即使在此之前,它实际上已经被废弃了七年,无论如何它从来没有足够完整以作为可行的替代 Java 实现。

去寻找另一个 Java AOT 编译器。

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

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

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.

虽然在Java中不是一切都GCJ支持下,特别是在GUI组件(见 什么的Java API的支持?如何完全是支持?从这个问题FAQ)。我没有经常使用 GCJ,但从我对控制台应用程序所做的有限测试来看,它似乎很好。

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

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

In terms of open-source installers, the Nullsoft Scriptable Install Systemis a scriptable installer. If you're curious, there are user contributed exampleson 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.)

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

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

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

回答by the.duckman

Launch4j

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

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

– Launch4j 的网站

回答by Stroboskop

We're using Install4Jto build installers for windows or unix environments.

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

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.

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

  • 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
  • 安装程序 + 启动程序
  • Windows 或 Unix
  • 可在 Java 中编写脚本
  • 蚂蚁任务
  • 许多可定制的标准面板和动作
  • 可选择包含或下载 JRE
  • 也可以启动windows服务
  • 多种语言

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

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

PS: sadly i'm not getting paid for this endorsement. I just like that tool.

PS:很遗憾,我没有因为这个代言而获得报酬。我就是喜欢那个工具。

回答by Thorbj?rn Ravn Andersen

The latest Java Web Starthas been enhanced to allow good offline operation as well as allowing "local installation". It is worth looking into.

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

EDIT 2018: Java Web Start is no longer bundled with the newest JDK's. Oracle is pushing towards a "deploy your app locally with an enclosed JRE" model instead.

编辑 2018:Java Web Start 不再与最新的 JDK 捆绑在一起。Oracle 正在推动“使用封闭的 JRE 在本地部署您的应用程序”模型。

回答by mdma

If you need to convert your entire application to native code, i.e. an EXE plus DLLs, there is ExcelsiorJET. I found it works well and provided an alternative to bundling a JRE.

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

回答by Ivan Maidanski

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.

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

回答by ErrorNotFoundException

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.

我反对将 java 程序转换为 .exe 应用程序是可以原谅的,我有我的理由。主要的一个是 Java 程序可以从很多 IDE 编译成 jar 文件。当程序为 .jar 格式时,它可以在多个平台上运行,而 .exe 只能在非常有限的环境中运行。我赞成 Java 程序不应转换为 Exe 的想法,除非非常必要。人们总是可以编写运行 Java 程序的 .bat 文件,而它是一个 jar 文件。

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.

如果确实需要将其转换为 exe,Jar2Exe 转换器会默默地执行此操作,并且还可以附加与主应用程序一起编译的库。

回答by Pratanu Mandal

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.

我会说 launch4j 是将 java 源代码 (.java) 转换为 .exe 文件的最佳工具您甚至可以将 jre 与它捆绑在一起进行分发,甚至可以将 exe 图标化。尽管应用程序的大小增加了,但它可以确保即使用户没有安装 jre 应用程序也能完美运行。它还确保您能够提供应用所需的特定 jre,而无需用户单独安装它。但不幸的是,java 失去了它的重要性。它的多平台支持完全被忽略,最终的应用程序只支持 windows。但这没什么大不了的,如果您只迎合 Windows 用户。