Paul Rowe
Paul Rowe

Reputation: 71

how to reference tools.jar from an executable jar

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?

Upvotes: 3

Views: 1570

Answers (3)

Paul Rowe
Paul Rowe

Reputation: 1

I dont want the manifest to reference system dependent locations. I also cannot use -classpath when using -jar. I think the solution to this is using -classpath and not using -jar. Rather than putting the Main-Class in the manifest, I will put the main class on the command line.

Upvotes: 0

Pram
Pram

Reputation: 2261

The -jar option takes precedence over the -classpath option. You can however include a classpath reference in the manifest file of the jar that is being executed. There is an attribute called "Class-Path" which can contain relative or absolute references to other jars

Here's a snippet to show you how

<target name="jar" depends="compile">
    <jar destfile="foo.jar" basedir="classes">
        <manifest>
            <attribute name="Main-Class" value="com.wiggle.foo" />
            <attribute name="Class-Path" 
                value="C:/Java/jdk1.5.0_21/lib/tools.jar" />
        </manifest>
    </jar>
</target>

Note that if you want to include multiple jars in the Class-Path attribute that they have to be on a single line in the manifest file

Upvotes: 0

Vineet Reynolds
Vineet Reynolds

Reputation: 76719

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.

Upvotes: 3

Related Questions