By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

Problem with JAR

P: n/a
Hello everyone.
I have the following problem. I am sure that I am doing something very
silly, but I cannot figure out what it is.

I have the following structure for my Java application:

1) These are the directories I subdivided my code into:
../images
../sound
../libraries
../classes

and this is the main class

MAIN.class

'classes' contains the byte code of the program I wrote; 'libraries'
contains two .jar files that must be included in the classpath for my
application to run (actually to send e-mails). 'images' and 'sounds'
contain, respectively, images and sounds used in the application.

2) If I run the main class, the ./MAIN.class, from a normal shell everything
works fine (obviously setting the classpath).

3) If I package the classes in a .jar file, the application starts but:
- the two .jar files in libraries are not in the classpath causing the
application to receive an exception when trying to use classes in them;
- none of the files in the directories ./sound and ./images is
accessible. The relative path used in the various files in the 'classes'
directory, that is 'images/img1.jpg', works fine only if the class is not in
a .jar.

The .jar files has been created with the following command line:

jar cvfm mymanifest appl.jar MAIN.class classes libraries images sounds

where mymanifest is composed by the following lines:

Main-Class: MAIN
Class-Path: . libraries/mail.jar libraries/activation.jar

I do hope to find someone able and willing to help me.

Thank you in advance,
F.A. Cardillo
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Franco Alberto Cardillo wrote:
Hello everyone.

3) If I package the classes in a .jar file, the application starts but:
- the two .jar files in libraries are not in the classpath causing the
application to receive an exception when trying to use classes in them;
- none of the files in the directories ./sound and ./images is
accessible. The relative path used in the various files in the 'classes'
directory, that is 'images/img1.jpg', works fine only if the class is not in
a .jar.

The .jar files has been created with the following command line:

jar cvfm mymanifest appl.jar MAIN.class classes libraries images sounds

where mymanifest is composed by the following lines:

Main-Class: MAIN
Class-Path: . libraries/mail.jar libraries/activation.jar

I do hope to find someone able and willing to help me.


Franco,

Two points:

1) Please note that the Class-Path attribute of the manifest should
contain classpath entries expressed relative to the jar file itself, not
the contents of the jar file. That is, if the jar file is
/home/franco/appl/appl.jar, the effective classpath is:

/home/franco/appl/appl.jar:/home/franco/appl:/home/franco/appl/libraries/mail.jar:/home/franco/appl/libraries/activation.jar.

Note also that the executed jar is in the classpath as well.

2) I would consider it bad practice to access resources such as images
via a relative path depending on the current working directory. This is
the source of your other problem. The correct way (IMHO, of course) is
to use the Class.getResource(), Class.getResourceAsStream(),
ClassLoader.getResource() or ClassLoader.getResourceAsStream() methods
instead. When using these methods, you place the needed resources in
the classpath (in your case, in the appl.jar file) and access them
"relative" to the classpath. (There are some subtleties here with the
paths and exactly which method you use.) For example:

final ClassLoader loader = this.getClass().getClassLoader();
final URL go = loader.getResource("/images/go.gif");

HTH,
Ray
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.