Home > Mobile >  What JDK should I compile with to support most desktop users?
What JDK should I compile with to support most desktop users?

Time:07-14

I write a few small, free, desktop command-line applications in Java. I package those as JAR files in releases on GitHub. About a year ago in light of Oracle licensing changes, I switched from the Oracle JDK to Open JDK. Developing on Windows, this is what I currently have installed:

C:\Users\admin>java -version 
openjdk version "17.0.1" 2021-10-19 
OpenJDK Runtime Environment (build 17.0.1 12-39) 
OpenJDK 64-Bit Server VM (build 17.0.1 12-39, mixed mode, sharing)

Now, about a week ago I was visiting a friend (also a software developer) and a reason came up where we wanted to run my application on his Windows box. He didn't have Java installed. So, watching over his shoulder, he went to the java.com "Download Java for Windows" page (currently listing Java Version 8 Update 341), downloaded, and installed it.

Then when he went to run my application, it failed to run, giving back an error along the lines of (paraphrasing from memory), "This version of the JRE does not support a later version of Java". This surprised both of us; he didn't know there was a later version of Java, and I didn't know compiling with the current OpenJDK would make a non-supported binary.

What's the best practice to fix this release problem?

CodePudding user response:

Well, you have a few options...

First of all you can TARGET the version 8 runtime, but you can compile code from later revisions of the language. This may or may not work in all cases, as Java 9 and up do some things rather differently! Still, fairly vanilla Java that isn't doing weird ClassLoader stuff is LIKELY to work, and you can certainly avoid problematic constructs.

Secondly, you can simply stick to Java 8! It is ANCIENT but it is a virtually immortal LTS, due to the reason above that things in Java 9 are different. However, you will miss out on new things.

You COULD go whole hog and move on from Java 17 to GraalVM, which can be had in versions compatible with Java11, Java17, etc. It has the ability to compile code down to a completely stand-alone binary, using native-image, and again unless you do some fairly esoteric stuff, your code will work. The end result will be similar to using something like C . You can even build shareable libraries.

I guess your other option is to just make sure people are not using Java8. Ideally they're using Java11, but I guess now java17 is the newest LTS, though few people seem to install it.

CodePudding user response:

Your user most likely ran into the issue that Java classes compiled with a newer class file version number do not run on older JVMs. If that is the only issue it can be address by recompiling ...

But there is a deeper issue. Older Java class libraries don't support all of the APIs provided by newer versions of Java. Also there have been some important architectural changes starting in Java 9 (e.g. addition of modules, removal of applets and closing off access to JDK internal classes) that "break" applications that run on older Java versions

What this means is that if you develop and test your code on Java 17 (say) there is a significant chance that it won't work on (say) Java 8 ... even if you compiled it for Java 8. And vice versa, because some APIs have been removed, or made inaccessible or ... work differently.

So my advice would be:

  1. Decide on a specific range of Java versions you will support for your application; e.g. Java 8 LTS1 through Java 17 LTS.

  2. Develop targeting the oldest Java version and its APIs.

  3. Build and test on the Java oldest version.

  4. Also test on (at least) all of the other LTS Java versions ... within the range you are supporting.

This will mean that you are limited to using the APIs and Java language features of your oldest supported version. This could hold you back, so you have to choose between that and supporting users with old (out of date) versions of Java.

The issue of users trying to install / use old versions of Java can be addressed in three ways:

  • Provide clear installation instructions to the user that say what kind / version(s) of Java they need to install, and where to get them from. (And how to set or configure JAVA_HOME if your application relies on that.)

    Of course, some users won't read the instructions properly, but that is their lookout ...

  • Use jlink to turn your application into a custom JVM, and distribute your code that way.

  • Use jpackage to create platform specific binaries.

If you take the jlink or jpackage routes, the onus will be on you to push out new distributables whenever there are Java security patches that are relevant to your application. Your users won't be able to "just install the latest Java patches" anymore.

Note that jlink is available for Java 9 onwards, and jpackage from Java 16 onwards.


What JDK should I compile with to support most desktop users?

I don't think there is a good answer to that. We can't tell you what proportion of "desktop" users have each version of Java installed. (Or what they are permitted to install; e.g. by corporate policy.) But you can't support old Java versions indefinitely.

I did find this though:


1 - Java 7 and earlier are all well beyond "end of life". You are not helping anyone by trying to support them.

CodePudding user response:

It looks like you are using Java 17. Developers of apps targeting Java version after 8 are expected to supply the runtime for running the application. This means there is no more 'downloading and installing Java' separately on the user side. This is also why the download page you refer to only offers Java 8.

In practice, this means that you should use jlink to create a runtime image (i.e. the thing that you would previously download and install) that can run your application.

jpackage can also be used to create application images and installers (it calls jlink under the hood). Both of those are tools that come with the JDK.

For your purposes I recommend using jpackage with something like this:

jpackage `
  --win-console `
  --main-jar app.jar `
  --main-class Main `
  --name myapp `
  --type app-image `
  --input input

In this command, input is a folder that has the main app.jar in it (Note: the input folder should not be the current directory, since that will lead to infinite recursion).

--win-console is also needed for console applications on Windows, since otherwise no console is created when running the app.

This command will create a myapp folder that you can zip/tar up and distribute.

This myapp folder has a myapp.exe launcher that can be used to run the application.

Also, note that this will create a runtime image with a default set of modules. If your jar is modular (i.e. it has a module-info file), I suggest using --module instead of --main-jar/--main-class, since that will use the module descriptor to determine the set of modules in the runtime image. (See jpackage --help)


Note that on Windows you will also need to install the wix toolkit. It can be installed easily through e.g. scoop:

scoop install wixtoolset
  •  Tags:  
  • java
  • Related