Starting java with -jar option precludes *.class files on CLASSPATH ?

L

Lukas

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
 
A

Andrew E

Lukas said:
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.

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..
 
L

Lukas

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.
 
T

Timbo

Lukas said:
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?
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
 
L

Lukas

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
 
T

Timbo

Lukas said:
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
 
L

Lukas

Indeed that helped! Apparently some misconception about the -jar option
had got stuck in my head there.

Thanks!

Lukas
 
N

Nigel Wade

Lukas said:
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?

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.
 
C

Chris Uppal

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
 
L

Lukas

The java command a user enters to execute the jar is not 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.
 
T

Timbo

Chris said:
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?
 
C

Chris Uppal

Timbo said:
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
 
T

Timbo

Chris said:
Timbo wrote:




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).
I *think* this is the case. Where else can the classes come from?
Certainly if you specify -jar, the classpath is ignored.

Tim
 
L

Lukas

is unambiguously claiming that there is no other source of user-defined
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
 
P

pkriens

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
 
R

Roedy Green

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
 
T

Timbo

Lukas said:
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 ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top