問題描述
如何將我的 Java 程序轉換為 .exe 文件? (How can I convert my Java program to an .exe file?)
如果我有 Java 源文件 (*.java) 或類文件 (*.class),如何將其轉換為 .exe 文件?
我還需要一個安裝程序程序。
參考解法
方法 1:
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 Excelsior's article "Convert Java to EXE – Why, When, When Not and How".
See also the companion article "Best JAR to EXE Conversion Tools, Free and Commercial".
方法 2:
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
方法 3:
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?"
方法 4:
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.
方法 5:
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).
PS: sadly i'm not getting paid for this endorsement. I just like that tool.
(by Waseem、Jay、the.duckman、coobird、user2675678、Stroboskop)