How can I package a Java desktop application?
Asked Answered
Z

9

15

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?

Zweig answered 27/10, 2011 at 11:38 Comment(0)
T
18

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.

There answered 27/10, 2011 at 11:49 Comment(1)
Update: Java Web Start is being phased out by Oracle. See their white paper, Java Client Roadmap Update dated 2020-05-11. One alternative is the open-source implementation of Java Web Start known as the OpenWebStart project.Abnegate
A
13

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:

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.

Abnegate answered 19/6, 2020 at 1:21 Comment(2)
Just out of curiosity: (I am not so familiar with Java) how big is the JVM that you bundle with a desktop application this way? Is there a way to trim down the bundled JRE libraries to only those needed by the app? I seem to remember this being a general complaint, that bundling the JRE makes app packages laughably massive.Trencherman
@Trencherman Use jlink (mentioned in Answer) to omit many of the 75 modules bundled with a JVM, if not needed by your particular app. As for size, I’ve seen reports of the runtime being about 40 megs, a seven-fold reduction from a full JDK — though your mileage may vary.Abnegate
W
5

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.

Williemaewillies answered 27/10, 2011 at 11:40 Comment(5)
I'm not convinced. By the sounds of things the ideal solution would provide separate packages per OS that will run without Java and offer to install a JRE if not currently present as well as having an OS specific way of launching the software. Can you do something anything like that with web start?Vaporish
Don't really understad the comment, well....why would you need separate packages per OS, java is like do it once run it everywhere with JRE, if you want something OS independent, do two java apps.. With java web start you can choose to run the application locally or run it from the web...Williemaewillies
Well you don't need a separate package per OS if JWS integrates in and looks like a native program. I didn't think it would run without having Java previously installed however (in which case you need native code, hence separate packages per OS). If JWS can run without a JVM on the client then that's fine.Vaporish
Great answer! There are some more handy links available in the JWS info. page. BTW - just discovered that link to the Developers Guide is available in the Java 7 docs. and updated the link. The pages are slightly different as well.Secunderabad
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.Abnegate
S
4

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.

Secunderabad answered 27/10, 2011 at 12:56 Comment(0)
B
3

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

Bethanie answered 23/4, 2021 at 23:37 Comment(0)
S
2

Are you looking for something like this:

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

?

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

Sherrell answered 27/10, 2011 at 11:40 Comment(0)
S
2

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.

Squinch answered 27/10, 2011 at 11:44 Comment(0)
A
1

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

Ahriman answered 27/10, 2011 at 11:49 Comment(0)
R
0

As of 2022 there is a new option, Hydraulic Conveyor. It's got a bunch of features that are useful for shipping JVM desktop apps:

  • It creates self-updating packages for Windows/Mac/Linux, using MSIX on Windows and Sparkle on macOS and native package managers on Linux.
  • It can ship for every OS from any OS i.e. you can ship to Mac/Windows from Linux, or Windows/Linux from Mac etc.
  • It runs jlink for you for each OS.
  • It provides binary launchers (no batch files or shell scripts) which set up the JVM environment in useful ways, like with computed system properties for your install dir, your update site URL.
  • Apps can update in the background or for Mac/Windows installs you can force the app to update automatically on startup if there's a new version available, like a web app would.
  • It handles code signing and notarization for you.
  • It generates a download page for your users and can use GitHub Releases as a download and update site.

So, if you want to ship a JVM desktop app it's got you covered. There's also samples showing how to ship apps that use JavaFX and Jetpack Compose for Desktop.

Reprint answered 27/2, 2023 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.