简体   繁体   中英

How can I create a Windows .exe (standalone executable) using Java/Eclipse?

I'm new to the programming scene. Been working with C++ for about 5 months now, and have decided I want to start getting into Java. I'm using Eclipse as my IDE, and obviously Java for the language. I'm trying to write a simple HelloWorld application, which can be run through a command prompt executable.

In Visual Studio, it seems it's rather easy to create an executable. All I've ever had to do is use a pull down arrow and choose Release, and then run my build. The purpose of wanting to be able to write/run Java in command prompt is so that I'm able to practice some of the language basics before I go in full force with Swing.

So bottom line, what is the easiest way to create a command prompt.exe written with Java?

Thanks in advance!

Java doesn't natively allow building of an exe , that would defeat its purpose of being cross-platform.

AFAIK, these are your options:

  1. Make a runnable JAR . If the system supports it and is configured appropriately, in a GUI, double clicking the JAR will launch the app. Another option would be to write a launcher shell script/batch file which will start your JAR with the appropriate parameters

  2. There also executable wrappers - see How can I convert my Java program to an .exe file?

See also: Convert Java to EXE: Why, When, When Not and How

Typical Java programs compile into .jar files, which can be executed like .exe files provided the target machine has Java installed and that Java is in its PATH. From Eclipse you use the Export menu item from the File menu.

Creating a native installer using jpackage

A java packaging tool named jpackage was released as part of the Java Development Kit (JDK) version 16.

This tool works in conjunction with native packaging tools for various platforms (eg WIX for Windows, RPM , and DEB for Linux distributions, DMG for Mac) to allow building native installers for Java applications which can then be run as executables. For distribution, it may be possible to distribute just the executable for the application, independent of the installer (I don't know, I didn't try that).

A nice, tutorial style, blog post that describes the use of the jpackage tool to create a native Windows installer for a Java application is:

Customizing the runtime image using jlink and jdeps

The packaging tool can (optionally) be combined with the jlink tool:

jlink - assemble and optimize a set of modules and their dependencies into a custom runtime image

This allows you to customize the runtime image for your application to only include the required custom selected modular parts of your application code, java runtime, and 3rd party libraries, rather than distributing a complete java runtime.

Optionally, you can also use the jdeps tool to determine inputs to jlink .

Complete discussion of usage of jdeps + jlink + jpackage + a native bundle creator (eg wix/rpm/deb/dmg) is outside of scope for a StackOverflow answer, but various resources can be found on the web if you search.

Creating a standalone .exe instead of an installer

This can be done using warp-packer to create a exe out of the image and app launcher created by jlink .

Third party tools can help deliver a solution

If you wish to use all these tools in combination, things can get complicated, and I'd advise using a 3rd party utility or template to help perform this task. For example:

  • badass-jlink-plugin ( documentation ) -> "allows you to create custom runtime images for modular applications with minimal effort. It also lets you create an application installer with the jpackage tool."

For JavaFX specific applications (as your question has a JavaFX tag), you could review:

  • JPackageScriptFX -> "demonstrates how projects can use scripts to build self-contained, platform-specific executables and installers of their JavaFX applications via the jdeps, jlink, and jpackage tools."

For native mobile deployments, see Gluon Mobile

This question was Windows specific, but for completeness, if your target is native application deployment on a mobile device, then likely you will need to use a 3rd party solution such as Gluon Mobile .


Background Rationale (ignore if not needed)

creating a native installer using jpackage might be a bit of work, so why would you do it?

Here are some reasons:

  1. When an application is distributed via an installer created using jpackage , that application can be installed on a target system without requiring the user to manually install other dependencies (such as a Java Runtime). The installer will take care of ensuring that your application and any dependencies it requires are installed.
  2. The application ships with its own customized version of the Java Runtime. This means that if the user doesn't install a JRE, and, if they have installed a version of the JRE which is incompatible with your application, your application will still function correctly.
  3. The application can be installed and uninstalled using standard OS facilities for the target platform. Most users of those platforms are familiar with these.

Yes, Java is cross-platform for the most part, but many users don't care much about that, instead they just want a smooth and familiar installation (and uninstallation) experience for their application and jpackage can help accomplish that.

Creating .exe distributions isn't typical for Java. While such wrappers do exist, the normal mode of operation is to create a .jar file.

To create a .jar file from a Java project in Eclipse, use file->export->java->Jar file. This will create an archive with all your classes.

On the command prompt, use invocation like the following:

java -cp myapp.jar foo.bar.MyMainClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM