Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I package a Java desktop application?

I am working on a simple desktop java application. I would like to make it as seamless to install for end users as possible. E.g. similar to how Minecraft is distributed - a simple executable for OS X and an EXE file for Windows.

What tool should I use?

like image 530
Niklas Avatar asked Oct 27 '11 11:10

Niklas


People also ask

How do I deploy a Java desktop application?

We will show a few different approaches for deploying an application, so that users can access the application by: Double-clicking the application's Java Archive (JAR) file. Calling the application from the command line. Calling the application from a script file.

Can you build desktop apps with Java?

Java is used to build robust and platform independent applications in many domains. This language is used for developing Android applications, web applications, desktop applications and many more.

How do I package a Java project?

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.

How do you package and distribute Java application?

The Java Packager tool can be used to compile, package, sign, and deploy Java and JavaFX applications from the command line. It can be used as an alternative to an Ant task or building the applications in an IDE. FXLauncher is both a launcher and an auto-updater that focuses solely on JavaFX applications.


8 Answers

Users of your Java app must have the JRE installed in order to run it.
You can either tell them to install Java first, or distribute JRE with your app, as Processing does.
Note, however, that your packaged program will be heavy if you include JRE with it. And, if you want to do that, users will need to download the appropiate package for their platform.

Executable Java Wrappers

They take your Java app as input and wrap them in an executable (for a specified platform). You can customize them as you like; and if the user doesn't have Java installed, the download page will open.

Some examples are Launch4J, JSmooth and Jar2EXE.
 

Installers

They are independent applications configured to copy your app files to the user's computer and (optionally) create a shortcut.

Some installers are written in Java, so they're multiplatform. In this case, the installer is a .jar.
Some others are platform-dependent, but you have the advantage that you don't need to wrap them.

Java installers: IzPack, Packlet, PackJacket, Antigen, …
 

Java Web Start

It's a Java feature that allows you users to easily run your apps. You give them a .jnpl file,
they open it, and Java downloads the latest version of your app and runs it. No packaging troubles!


See the complete list of resources here.

like image 121
Alba Mendez Avatar answered Oct 20 '22 01:10

Alba Mendez


Bundle JVM within your app

The modern solution to deploying a Java-based desktop app is to bundle a JVM with your app, delivered to the user as a double-clickable package just like other "normal" apps. Think of the JVM as another dependency to be incorporated within your final app.

Learn about:

  • Java Platform Module System
  • jlink (JEP 282)
  • JEP 220: Modular Run-Time Images
  • jpackage (JEP 343)

You could ship a JavaFX (OpenJFX) app this way, as well as a Swing app (the predecessor to JavaFX).

One advantage of this approach is that you know exactly what JVM is being used to run your particular app. Of course it also means you must release a new version of your app when a JVM update contains a relevant fix. Your users no longer are concerned with downloading, installing, and upgrading a JVM, as that chore is now the responsibility of the developer.

One disadvantage of this approach is that you must build, test, and distribute separate binaries of your app, one for each platform (macOS, MS Windows, BSD, Linux, and so on).

See also my Answer on a related Question, How to use jdk without jre in Java 11.

See the Answer by Will Iverson for what looks like some useful tooling.

Compiled native app

GraalVM

A cutting-edge variation of this approach is to build an ahead-of-time-compiled (versus JIT) native-code version of your app using GraalVM and its native image generator.

Project Leyden

Possibly related: Oracle's Project Leyden. See initial announcement by Mark Reinhold, and see this article, 2020-05-07.

Java Web Start phase-out

Some other Answers discuss Java Web Start. Be aware that Java Web Start and Java Applets technologies are both being phased out by Oracle. See their white paper, Java Client Roadmap Update dated 2020-05-11.

A possible alternative is the open-source implementation of Java Web Start known as the OpenWebStart project.

like image 44
Basil Bourque Avatar answered Oct 20 '22 03:10

Basil Bourque


Java Web Start I think is the option you're looking for...

Edit

Comment from Basil Bourque @ 2020:

Java Web Start is being phased out by Oracle. See their white paper, Java Client Roadmap Update dated 2020-05-11. You may be interested in the open-source implementation of Java Web Start known as the OpenWebStart project.

like image 36
jjchiw Avatar answered Oct 20 '22 01:10

jjchiw


Edit: The Java Plug-In, required for both applets and Java Web Start, was deprecated in Java 9 and removed from browsers around the same time. So this answer is obsolete. Only leaving it here to warn people against following this path.


The optimal installation for a cross-platform app. with a GUI is to be found in Java Web Start. To quote the linked document in part:

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update ..

Other matters

..an OS specific way of launching the software.

Adding a shortcut (with icon) to the desktop or start/program menu (desktop integration) is about as platform specific as you can get.

..will run without Java and offer to install a JRE if not currently present

Use deployJava.js, linked in the 2nd last list item under 'See also:'.

Well you don't need a separate package per OS if JWS integrates in and looks like a native program.

Using the native PLAF would be a good start.

like image 28
Andrew Thompson Avatar answered Oct 20 '22 01:10

Andrew Thompson


If you are using Java 16, jlink and jpackage will do what you need - build a desktop application and installer.

Here is a GitHub template that uses Maven to build a small / reduced JVM + JavaFX application. It uses Java modules and jlink to produce the small JVM, but uses the normal class path for running your application. GitHub Actions are used to produce the installers for macOS, Windows, and Linux.

https://github.com/wiverson/maven-jpackage-template

Here is another (WIP) version of the template with a small Swing UI and Spring Boot underneath - no JavaFX.

https://github.com/wiverson/desktop-spring-boot

like image 45
Will Iverson Avatar answered Oct 20 '22 02:10

Will Iverson


Are you looking for something like this:

http://www.regexlab.com/en/jar2exe/

?

That allows the enduser to run the .exe without installing.

like image 31
Rohan Avatar answered Oct 20 '22 02:10

Rohan


There are a number of options - Java web Start, 3rd party installer, etc.

Have a look at http://mindprod.com/jgloss/installer.html for an explanation.

like image 38
Paul Cager Avatar answered Oct 20 '22 03:10

Paul Cager


I think that you're looking for IzPak. There is even a GUI available called PackJacket

like image 40
Baltasarq Avatar answered Oct 20 '22 01:10

Baltasarq