use-newer-java

A small lib to give your users a friendly message that they're using a version of Java that's too old.

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.madgag
ArtifactId

ArtifactId

use-newer-java
Last Version

Last Version

0.8
Release Date

Release Date

Type

Type

jar
Description

Description

use-newer-java
A small lib to give your users a friendly message that they're using a version of Java that's too old.
Project URL

Project URL

https://github.com/rtyley/use-newer-java
Project Organization

Project Organization

com.madgag
Source Code Management

Source Code Management

https://github.com/rtyley/use-newer-java

Download use-newer-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.madgag/use-newer-java/ -->
<dependency>
    <groupId>com.madgag</groupId>
    <artifactId>use-newer-java</artifactId>
    <version>0.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.madgag/use-newer-java/
implementation 'com.madgag:use-newer-java:0.8'
// https://jarcasting.com/artifacts/com.madgag/use-newer-java/
implementation ("com.madgag:use-newer-java:0.8")
'com.madgag:use-newer-java:jar:0.8'
<dependency org="com.madgag" name="use-newer-java" rev="0.8">
  <artifact name="use-newer-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.madgag', module='use-newer-java', version='0.8')
)
libraryDependencies += "com.madgag" % "use-newer-java" % "0.8"
[com.madgag/use-newer-java "0.8"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.13.5

test (2)

Group / Artifact Type Version
junit : junit jar 4.13.2
com.novocode : junit-interface jar 0.11

Project Modules

There are no modules declared in this project.

Use-Newer-Java

Maven Central

A small helper library to give users a friendly message when they're using a version of Java that's too old

The Problem...

Does your app require a recent version of Java? Are your users likely to have only old versions of Java installed?

If users run code with a version of Java that's too old, the fatal error message isn't friendly - it starts like this: java.lang.UnsupportedClassVersionError : Unsupported major.minor version 51.0... and, because it's a stacktrace, carries on for several lines lines like this...

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/eclipse/jgit/lib/Repository : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    ...

...that's a bad message to show your user! UnsupportedClassVersionError means that their version of Java is too old, and major.minor version 51.0 means they need at least Java version 7 to get the code to run - but they probably won't know that.

How to give the user a better message?

Use Newer Java is a small library compiled for old versions of Java and designed for command-line Java tools like the BFG Repo-Cleaner, that are distributed as a single uber-jar containing all dependencies and run with the java -jar my-app.jar invocation.

It will display friendly messages like this to your user:

Looks like your version of Java (1.8) is too old to run this program.

You'll need to get Java 9 or later.

If the user has a version of Java that's new enough, the user won't see the message, and Use Newer Java will just pass along the command line arguments to the main class of your command-line app, which will run normally. Your app can be compiled for the latest version of Java - Use Newer Java will be the only code targeting old versions of Java in your app.

UseNewerJava is compiled targeting Java 5, meaning that it will execute correctly on most Java VMs users are likely to have these days.

How can I use this in my project?

  1. Include use-newer-java as a dependency in your project
  2. Set the Main-Class: field in the manifest of your built jar to use.newer.java.Version8 or use.newer.java.Version9, etc as appropriate.
  3. Set the Main-Class-After-UseNewerJava-Check: in your jar manifest to the normal main class of your app- Use Newer Java will follow this setting, and invoke this code after making it's check.

That's it.

Example

See https://github.com/rtyley/bfg-repo-cleaner/commit/a1cf8694f for an example of adding UseNewerJava to a CLI app within an SBT build. It shouldn't be too hard to take the same approach with Maven, Gradle builds etc.

Versions

Version
0.8
0.1