Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Adding a jar to the classpath of an executable jar (mixing -jar and -cp)

jduprezDec 7 2010 — edited Dec 8 2010
Hello,

frankly I hesitated over posting this to "New to Java"; my apologies (but also, eternal gratefulness) if there is an ultra-simple answer I have overlooked...

I integrate a black-box app (I'm not supposed to have the source) that comes packaged as an executable jar (with a Manifest.MF that specifies the main class and a bunch of dependent jars), along with a few dependent jars and a startup script. Long story short, the application code supports adding jars in the classpath, but I can't find a painless way to add a jar in its "classpath".

The app's "vendor" (another department of my customer company) has a slow turnaround on support requests, so while waiting for their suggestion as to how exactly to integrate custom jars, I'm trying to find a solution at the pure Java level.

The startup script features a "just run the jar" launch line:
java -jar startup.jar
I tried tweaking this line to add a custom jar in the classpath
java -cp mycustomclasses.jar -jar startup.jar
But that didn't seem to work ( NoClassDefFound at the point where the extension class is supposed to be loaded).

I tried various combination of order, -cp/-classpath, using the CLASSPATH environment variable,... and eventually gave up and devised a manual launch line, which obviously worked:
java -cp startup.jar;dependency1.jar;dependency2.jar;mycustomclasses.jar fully.qualified.name.of.StartupClass
I resent this approach though, which not only makes me have to know the main class of the app, but also forces me to specify all the dependencies explicitly (the whole content of the Manifest's class-path entry).

I'm surprised there isn't another approach: really, can't I mix -jar and -cp options?
- [url http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html]This document (apparently a bible on the CLASSPATH), pointed out by a repited forum member recently, does not document the -jar option.
- the [url http://download.oracle.com/javase/tutorial/deployment/jar/run.html]Java tutorial describes how to use the -jar option, but does not mention how it could play along with -cp

Thanks in advance, and best regards,

J.

Edited by: jduprez on Dec 7, 2010 11:35 PM
Ahem, the "Java application launcher" page bundled with the JDK doc (http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html) specifies that +When you use [the -jar] option, the JAR file is the source of all user classes, and other user class path settings are ignored+
So this behavior is deliberate indeed... my chances diminish to find a way around other than specifying the full classpath and main class...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 5 2011
Added on Dec 7 2010
6 comments
1,376 views