reference to external jars when running jar file?

B

bo_kid

Hello all,

I'm currently working under Eclipse and I've created a jar file,
test.jar.
This jar file needs an extenal jar, let's say c:\ext.jar, to be able to
run. (It runs ok in Eclipse).
Because ext.jar can only/must be accessed with an absolute path, I
can't use the Class-Path header in the manifest. This is correct right?

So I try with to use the the -cp command parameter:
java -cp c:\ext.jar -jar test.jar

But this doesn't work! It seems like ext.jar isn't on the path!
I've read somewhere that when using the -jar parameter all classpath
parameters are ignored, is this correct?
If this is the case, how can I access the external jar file?
How do Eclipse do when it runs my jar-file?

Thanks for any clues!
/Kid
 
B

bo_kid

One more thing.

Let's say that the -jar argument ignores all -cp and -classpath
arguement.
How is the system classpath affected? It is still used right? Then I
could add the ext.jar to the system classpath (ugly).

/Kid
 
B

bo_kid

Thanks Tor, but like I said the ext.jar file is not in the same
directory as test.jar. In fact it must be accessed with an absolute
path and I don't think the Class-Path header manages absolute paths.
Any other ideas?

Best Regards,
/Kid
 
N

Nigel Wade

Thanks Tor, but like I said the ext.jar file is not in the same
directory as test.jar. In fact it must be accessed with an absolute
path and I don't think the Class-Path header manages absolute paths.

It certainly does.
 
B

bo_kid

Thanks for the replies.

The reason I'm saying that I don't think the Class-Path header manages
absolute paths is the following quote from
http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#JAR Manifest
:

# Class-Path :

The value of this attribute specifies the RELATIVE URLs of the
extensions or libraries that this application or extension needs. URLs
are separated by one or more spaces. The application or extension class
loader uses the value of this attribute to construct its internal
search path.

I've also tried it myself, but it's possible I made an syntax error.

So if I can have abolute paths in the Class-Path header how is the
syntax?
For windows I've tried with the following:
Class-Path: c:\ext.jar
Class-Path: c:/ext.jar

but with no luck.
Can anyone of you help me with this?
By the way how is the syntax for linux?
Class-Path: /ext.jar ?
 
N

Nigel Wade

Remi said:
Although it is certainly not recommended, and kind of defeats its
whole purpose

The whole purpose is for Java to be able to locate the jar files it
requires. If an absolute path is the best way to do this it has served its
purpose perfectly.
 
B

bo_kid

You guys might want to hear my reason for having absolute path in
Class-Path?

I'm incorporating OpenOffice in my java app. To do this I have to use
some jar files provided by OpenOffice. These jar-files must not be
moved from their location because they locate OpenOffice stuff relative
to their locataion.

Since the location for my application have nothing to do with
OpenOffice (they are located on two different partitions!) I must use
an absolute path to find OpenOffice stuff!

With this said, can anyone tell me how the syntax is when using
absolute paths in Class-Path? Both windows and linux.
Nigel?
Remi?

/Kid
 
N

Nigel Wade

Thanks for the replies.

The reason I'm saying that I don't think the Class-Path header manages
absolute paths is the following quote from
http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#JAR Manifest
:

# Class-Path :

The value of this attribute specifies the RELATIVE URLs of the
extensions or libraries that this application or extension needs. URLs
are separated by one or more spaces. The application or extension class
loader uses the value of this attribute to construct its internal
search path.

It accepts absolute paths on UNIX. Whether this is intentional, a bug, or a
"feature" I have no idea.
I've also tried it myself, but it's possible I made an syntax error.

So if I can have abolute paths in the Class-Path header how is the
syntax?
For windows I've tried with the following:
Class-Path: c:\ext.jar
Class-Path: c:/ext.jar

but with no luck.
Can anyone of you help me with this?
By the way how is the syntax for linux?
Class-Path: /ext.jar ?

In one particular case I have this Class-Path for Linux:

Class-Path: /home/nigel/Java/lib/imap.jar /home/nigel/Java/lib/mailapi
.jar /home/nigel/Java/lib/smtp.jar

How you would achieve the same for Windows I don't know. Windows is a law
unto itself when it comes to absolute paths, as it gets all mixed up with
drive letters, UNC paths and all the rest of the mess that is the Windows
system. My guess would be that if an absolute path designation will work on
the command line it will work in a manifest file, but I could well be
wrong.
 
A

Andrea Desole

You guys might want to hear my reason for having absolute path in
Class-Path?

I'm incorporating OpenOffice in my java app. To do this I have to use
some jar files provided by OpenOffice. These jar-files must not be
moved from their location because they locate OpenOffice stuff relative
to their locataion.

Since the location for my application have nothing to do with
OpenOffice (they are located on two different partitions!) I must use
an absolute path to find OpenOffice stuff!

With this said, can anyone tell me how the syntax is when using
absolute paths in Class-Path? Both windows and linux.

putting an absolute classpath in a jar doesn't really sound a good idea.
How about defining a URLClassLoader with the location of OpenOffice, and
use that one to load your classes?
 
B

bo_kid

Thanks for the info Nigel.

Andrea, that's maybe what I should have done the first place!
One question.
I'm little but unsure about the the following sentence in the API:
"...Otherwise, the URL is assumed to refer to a JAR file which will be
opened as needed."
If I create a new URLClassLoader with all the jars I need, do I then
have to manually load all the classes (located in the jars) I need or
is this done automatically when I'm instanciating the classes?
The problem is that I don't really know what classes in jars are used!

Best Regards,
/Kid
 
A

Andrea Desole

Thanks for the info Nigel.

Andrea, that's maybe what I should have done the first place!
One question.
I'm little but unsure about the the following sentence in the API:
"...Otherwise, the URL is assumed to refer to a JAR file which will be
opened as needed."
If I create a new URLClassLoader with all the jars I need, do I then
have to manually load all the classes (located in the jars) I need or
is this done automatically when I'm instanciating the classes?

as far as I know, when a class A loads another class B, it uses the
classloader that was used to load the class A itself. This means that
you should be able to use the class loader to load only the first
classes. The classes used by the classes loaded by you should be loaded
automatically. This should be written somewhere in the JVM specs.
I have never done it, but it should work. Hope it's clear...
 
B

bo_kid

Had some help from Roland de Ruiter using absolute path in the
Class-Path for windows. Java URLs, e.g. file:///C:/ext.jar, have to be
used! One should probably always use java URLs when specifying
classpaths.

Thanks for all the help and info everyone!
/Kid
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top