Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Java 8 jar when only Java 11 JRE is available

Tags:

java

java-11

I have a third-party executable jar file which I don't have access to the source code of. It was compiled with Java 8 and it uses some javax packages for XML processing. These are in Java EE and have been removed from recent versions of Java SE.

I want to run this third-party jar file on a host machine that I don't have control over. It has Java 11 installed and I'm not allowed to install Java 8 on it.

I've seen this answer which says that the way to solve this issue is to rebuild the application with additional dependencies to replace the Java EE packages that were removed from the Java 11 jre. Unfortunately, I can't use that answer because I don't have access to the source code. Can I instead use a -classpath argument to the java -jar command to solve this?

like image 237
DodgyCodeException Avatar asked Mar 13 '20 10:03

DodgyCodeException


People also ask

Can I use Java 11 jar Java 8?

Multi-release jar files allow you to support both Java 8 and Java 11 runtimes from the same jar file.

Is JRE required for Java 11?

In JDK 11, this is no longer an option. In this release, the JRE or Server JRE is no longer offered. Only the JDK is offered. Users can use jlink to create smaller custom runtimes.

Can newer JRE versions run Java programs compiled with older JDK versions?

You would not encounter any problems - that's the magic of Java -it's backwards compatible.


1 Answers

If it is a runnable jar, then it has a META-INF/MANIFEST.MF file that sets up the classpath.

You don't need the source code. Just unjar the jar file, add extra entries for the required additional third-party jars to the Class-Path in the MANIFEST.MF file, and jar it back up.

Then ship the jar file together with the additional third-party jars.


Example

Say your foo.jar file uses JAF (java.activation), i.e. it needs javax.activation-1.2.0.jar added to the classpath.

Edit the MANIFEST.MF file and add javax.activation-1.2.0.jar to the end of the Class-Path value, separated from existing values with a space. If there is no Class-Path, add it:

Class-Path: javax.activation-1.2.0.jar

Then ship the updated foo.jar and the new javax.activation-1.2.0.jar file, to be placed in the same folder.

like image 175
Andreas Avatar answered Oct 19 '22 19:10

Andreas