Java Forum / General / December 2005
Starting java with -jar option precludes *.class files on CLASSPATH ?
Lukas - 29 Nov 2005 11:58 GMT Hi group,
Since I found the documentation unclear on the issue, I ran some tests to see whether I could get a directory with *.class files onto the CLASSPATH when starting a JARred Java app (java -jar someApp.jar), using either the JARs Manifest file, java's -classpath option or by setting the CLASSPATH environment variable in a batch file.
It appears it can't be done.
A) can anyone confirm this?
B) are there any known reasons for this seemingly arbitrary restriction?
Cheers
YT
Andrew E - 29 Nov 2005 12:56 GMT > Hi group, > [quoted text clipped - 5 lines] > > It appears it can't be done. if you do this:
java -cp C:\wherever\classes -jar someApp.jar
dooes that work? I don't have a java-installation in front of me to test, but I'm sure that used to work for me. But I could be wrong..
Lukas - 29 Nov 2005 13:32 GMT I used relative paths, and found it doesn't work. .. Didn't test for absolute paths because for my purpose they'd not cut it, but I can't see why absolute vs relative should make a difference.
Also I've tried
set CLASSPATH=whereever;.
and in Manifest.mf
Class-Path: whereever .
They all don't work with -jar. As soon as you do
java -cp C:\wherever\classes someApp.class
everything works. I'd rather be wrong about this though.
Roedy Green - 29 Nov 2005 17:44 GMT >if you do this: > > java -cp C:\wherever\classes -jar someApp.jar > >dooes that work? when you use a jar, the classpath is ignored. Sun assures us this is not a bug but a feature.
see http://mindprod.com/jgloss/classpath.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Timbo - 29 Nov 2005 14:24 GMT > Hi group, > [quoted text clipped - 7 lines] > > A) can anyone confirm this? Yes, the -cp option is ignored when the -jar option is used
> B) are there any known reasons for this seemingly arbitrary > restriction? Because jars are designed to be self contained.
The easiest way around it is: java -cp normal_class_path:someApp.jar the,main.method.Class
It does mean the main method property in the manifest file is ignored, but if you rely on some third-party app that can't be jarred up with your app, then I'm afraid your stuck with this, or something similar.
Cheers, Tim
Lukas - 29 Nov 2005 16:22 GMT Hi Tim,
that's how I'm currently dealing with the problem, and yes, it works just fine, it only looks kinda unprofessional to have a full
com/somecompany/someproject/EntryPoint.class
lying around in your install directory when you were hoping to just have
lib/someproject.jar
looking neat and tidy. Hybris, I guess.
Thanks,
Lukas
Timbo - 29 Nov 2005 16:47 GMT > Hi Tim, > > that's how I'm currently dealing with the problem, and yes, it works > just fine, it only looks kinda unprofessional to have a full > > com/somecompany/someproject/EntryPoint.class You don't need to have the classes unjarred into the install directory. If you include the jars from your app and the third party app in the -cp option and then just provide the name of the class with the 'main' method: java -cp thirdparty.jar:yourApp.jar your.main.Class
then your classpath becomes all of the classes in both jar files. It's still not ideal, but it's better than having the classes jarred our into the install directory.
Hope this helps! Tim
Lukas - 30 Nov 2005 10:27 GMT Indeed that helped! Apparently some misconception about the -jar option had got stuck in my head there.
Thanks!
Lukas
Nigel Wade - 30 Nov 2005 10:59 GMT > Hi group, > [quoted text clipped - 7 lines] > > A) can anyone confirm this? Yes. The documentation is pretty clear about this. From the java man page (does Windows have an equivalent?):
-jar Executes a program encapsulated in a JAR archive. ... When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
> B) are there any known reasons for this seemingly arbitrary > restriction? The java command a user enters to execute the jar is not under your control.
Use the manifest to set the class-path for your jar, that is under your control.
 Signature Nigel Wade, System Administrator, Space Plasma Physics Group, University of Leicester, Leicester, LE1 7RH, UK E-mail : nmw@ion.le.ac.uk Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Lukas - 30 Nov 2005 15:49 GMT >> The java command a user enters to execute the jar is not under your control. >> Use the manifest to set the class-path for your jar, that is under your control. Indeed, I preferred using the manifest file to set the classpath and I do understand the concept of encapsulation that's enforced there.
The 'arbitrary restriction' I was referring to is that, when using the manifest, you can only put Jars on the classpath, not directories with lose class files in them.
pkriens@gmail.com - 30 Nov 2005 11:18 GMT Heard of manuals?
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
-jar Execute a program encapsulated in a JAR file. The first argument is the ... ... Jar files and Jar-file manifests. When you use this option, the JAR file is the source of all user classes, and OTHER USER CLASS PATH SETTINGS ARE IGNORED.
Emphasis mine ...
Kind regards,
Peter Kriens
Chris Uppal - 30 Nov 2005 12:14 GMT > When you use this option, the JAR file is the source of all user > classes, > and OTHER USER CLASS PATH SETTINGS ARE IGNORED. > > Emphasis mine ... Check the previous clause; that /states/ that application classes are not loaded from anywhere except that JAR file -- which is manifestly false. So, given that the documentation is wrong, what should the interpretation be ? Suppose we conclude that it means that the classpath is ignored; that still leaves open the question of whether it is possible to refer to external directories (as opposed to JAR files).
-- chris
Timbo - 01 Dec 2005 09:25 GMT >> When you use this option, the JAR file is the source of all user >>classes, [quoted text clipped - 4 lines] > Check the previous clause; that /states/ that application classes are not > loaded from anywhere except that JAR file -- which is manifestly false. Can you explain what you mean here? I think you are talking about loading classes from within your source code?
Chris Uppal - 01 Dec 2005 14:23 GMT > > Check the previous clause; that /states/ that application classes are > > not loaded from anywhere except that JAR file -- which is manifestly > > false. > > Can you explain what you mean here? I think you are talking about > loading classes from within your source code? I'm not sure what you are asking. But all I meant was that the sentence (fragment):
When you use this option, the JAR file is the source of all user classes,
is unambiguously claiming that there is no other source of user-defined classes, that only classes in /that/ JAR file will be available. Hence the application must be written using only the classes that are present in that JAR or which are part of the standard platform library. Which is not true (or so I believe -- I've never had occasion to test it).
-- chris
Timbo - 02 Dec 2005 09:43 GMT >>>Check the previous clause; that /states/ that application classes are >>>not loaded from anywhere except that JAR file -- which is manifestly [quoted text clipped - 13 lines] > or which are part of the standard platform library. Which is not true (or so I > believe -- I've never had occasion to test it). I *think* this is the case. Where else can the classes come from? Certainly if you specify -jar, the classpath is ignored.
Tim
Lukas - 02 Dec 2005 17:17 GMT > > is unambiguously claiming that there is no other source of user-defined > > classes, that only classes in /that/ JAR file will be available. Hence the [quoted text clipped - 4 lines] > I *think* this is the case. Where else can the classes come from? > Certainly if you specify -jar, the classpath is ignored. If you use the line "Class-Path:" within the JAR's manifest, you can specify other JARs to put on the classpath
Timbo - 07 Dec 2005 10:24 GMT >>>is unambiguously claiming that there is no other source of user-defined >>>classes, that only classes in /that/ JAR file will be available. Hence the [quoted text clipped - 7 lines] > If you use the line "Class-Path:" within the JAR's manifest, you can > specify other JARs to put on the classpath Ah, of course! Thanks for the tip ;-)
pkriens@gmail.com - 03 Dec 2005 11:23 GMT > Check the previous clause; that /states/ that application classes are not > loaded from anywhere except that JAR file -- which is manifestly false. What "application classes" are not loaded from the JAR file?
Kind regards,
Peter Kriens
Roedy Green - 03 Dec 2005 15:14 GMT >What "application classes" are not loaded from the JAR file? Classes in the ext directory. Sun classes (not considered application) classes mentioned in the Jar's Class-Path
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|