5

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?

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
  • 3
    The answer you’ve linked describes where you can get the classes (or jars) from. You don’t need to rebuild your application. You only need to build the library which provides the classes (I don’t know which of the projects listed in the answer you need), then you can add the jar file(s) to your classpath. Since `javax.…` is not a reserved package name in the security model, this works. – Holger Mar 13 '20 at 10:49

2 Answers2

6

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.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

java -jar does not allow to set custom classpath.

talex
  • 17,973
  • 3
  • 29
  • 66
  • Whilst your comment is true, it does not provide a solution to the question. – Silviu Burcea Mar 13 '20 at 10:56
  • 2
    Answer is true, but the classpath can be replaced, by not using `-jar`, so the answer is really not that useful. – Andreas Mar 13 '20 at 10:56
  • 1
    A workaround is a solution too. Maybe not an elegant one, but it is a solution. Simply saying "cannot be done" when a workaround is available is misleading. – Andreas Mar 13 '20 at 18:04