Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reference tools.jar from an executable jar

Tags:

java

say I am doing this:

java -jar someJar

"someJar" needs to reference tools.jar (for jsp compilation). I don't want to bundle tools.jar inside of someJar because I am afraid of a incompatibility with the executing JVM. I can't specify -classpath because it is ignored when -jar is used. I tried -Djava.ext.dirs and that did not work. Does anyone have an idea on how I can reference tools.jar from an executable jar?

like image 753
Paul Rowe Avatar asked Aug 16 '26 14:08

Paul Rowe


1 Answers

Starting with Java 1.4, tools.jar needs to be included in the user classpath:

The tools classes are now in a separate archive (tools.jar) and can only be used if included in the user class path (to be explained shortly).

In other words, tools.jar cannot be placed in an extensions directory. This effectively means that specifying it in the manifest of the JAR might not be a good idea at all. There are a couple of options however:

  1. Specify tools.jar using the CLASSPATH variable or the -cp option. This works if you have a main class inside the JAR, which can now be initialized using the java -cp $JDK_HOME/lib/tools.jar MainClass command, where MainClass is the fully qualified name of the class.
  2. Use a custom classloader that loads tools.jar, provided that you know the location of the JDK home directory. Using a JRE will make things difficult, as you would then be forced to request the user to specify the location of the JDK. The location of the JDK can be determined via the java.home property using the System.getProperty call.
like image 92
Vineet Reynolds Avatar answered Aug 18 '26 03:08

Vineet Reynolds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!