Jar and External Resources

S

Sergio B.

Hello,

how can I access, from a source code packed into a Jar, an "external"
resource which is in the classpath but is not in the Jar?
Moreover, in the same situation, I'm not able to access a jdbc database
driver whose Jar is in the classpath, unless I include it in my application
Jar!
Where is the problem?
My JDK is 1.4.2
Please help me.
Thank you,
Good Bye,

Sergio B.
 
T

Tony Morris

I am making the assumption that you are executing your JAR using the -jar
option.
This will ignore the system CLASSPATH and command line -classpath option.

You have a number of options to overcome this design intention:
- Package your JAR with the second jar and reference it in the Manifest with
a Class-Path entry (fugly)
- Unpack the jars and create a single jar (fugly, but it's been done a
countless number of times)
- Write your own ClassLoader implementation that loads classes from the
"external" jar (a nice solution which has been implemented many times e.g.
Apache Ant). A typical implementation simply loads all classes from jar
files located in a directory, such as "lib".
 
T

Tor Iver Wilhelmsen

Sergio B. said:
how can I access, from a source code packed into a Jar, an "external"
resource which is in the classpath but is not in the Jar?

Same as any other resource on the classpath, ClassLoader.getResource()
and getResourceAsStream().
Moreover, in the same situation, I'm not able to access a jdbc database
driver whose Jar is in the classpath, unless I include it in my application
Jar!

You should; are you sure you use the correct name with package and
everything?
 
S

Sergio B.

Tor Iver Wilhelmsen wrote:

[External resource]
Same as any other resource on the classpath, ClassLoader.getResource()
and getResourceAsStream().

Are you sure?
I also thought so, I tried...
I wrote:
String path=ClassLoader.getSystemResource("resource_name").getPath()
with the resource stored in a root directory of the classpath;
....but this works only if the source code isn't packed in a Jar; if it is,
the resource isn't found!
I cannot understand why!

[Database driver problem]
You should; are you sure you use the correct name with package and
everything?

Yes, because the code works if it's not packed in a Jar!
I use the usual:
Class.forName("org. ...")
with the driver Jar mounted in the classpath.
Any idea?
You might try by yourself and let me know...
Thank you for your support!
Good Bye,

Sergio B.
 
T

Tony Morris

You have source code in a JAR ? why ?

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software

Sergio B. said:
Tor Iver Wilhelmsen wrote:

[External resource]
Same as any other resource on the classpath, ClassLoader.getResource()
and getResourceAsStream().

Are you sure?
I also thought so, I tried...
I wrote:
String path=ClassLoader.getSystemResource("resource_name").getPath()
with the resource stored in a root directory of the classpath;
...but this works only if the source code isn't packed in a Jar; if it is,
the resource isn't found!
I cannot understand why!

[Database driver problem]
You should; are you sure you use the correct name with package and
everything?

Yes, because the code works if it's not packed in a Jar!
I use the usual:
Class.forName("org. ...")
with the driver Jar mounted in the classpath.
Any idea?
You might try by yourself and let me know...
Thank you for your support!
Good Bye,

Sergio B.
 
S

Sergio B.

Tony said:
I am making the assumption that you are executing your JAR using the -jar
option.

Yes, you are right!
This will ignore the system CLASSPATH and command line -classpath option.

I didn't know this...thank you!
You have a number of options to overcome this design intention:
- Package your JAR with the second jar and reference it in the Manifest
with a Class-Path entry (fugly)

I don't understand this very well...moreover, if my external resource isn't
a Jar, how can I do?
- Unpack the jars and create a single jar (fugly, but it's been done a
countless number of times)

I can't adopt this for my application...
- Write your own ClassLoader implementation that loads classes from the
"external" jar (a nice solution which has been implemented many times e.g.
Apache Ant). A typical implementation simply loads all classes from jar
files located in a directory, such as "lib".

Yes, nice solution, I'll try it and let you know...
Thank you very much,
Good Bye,

Sergio B.
 
T

Tor Iver Wilhelmsen

Sergio B. said:
String path=ClassLoader.getSystemResource("resource_name").getPath()

This is not what I suggested you should use. Read it again.
You might try by yourself and let me know...

No, I cannot recreate your particular setup with so little information.
 
S

Sergio B.

Tor said:
This is not what I suggested you should use. Read it again.

Ok, because you wrote "ClassLoader.getResource()" but this isn't a static
method, I think that you suggested either what I firstly wrote or maybe:

my_object.getClass().getClassLoader().getResource("...")

Well, this leads to the same problem.
If this isn't what you suggested, please be clearer.
No, I cannot recreate your particular setup with so little information.

There's no particular setup.
Create a simple application that access, with the methods you suggested, to
an external resource such as a gif icon, pack the application in a Jar and
store the resource in a root directory of the system CLASSPATH. Run with:
java -jar my_application
and you shouldn't be able to access the resource!
However, if you haven't enough time to test this, don't worry!
Thank you for your attention,
Good Bye,

Sergio B.
 
S

Sergio B.

Tor said:
Not without adding an entry to the manifest file like

Class-path: someOther.jar yetAnother.jar

Yes, here is the problem, like Tony Morris said!
Thank you,
Good Bye!

Sergio B.
 
S

Sergio B.

Hello Tony,

I'm actually solving my problem following your first suggestion, adding a
proper Class-Path entry in the MANIFEST.
This is actually the best and fastest solution for my application.
Thank you very very much for your information and for your help.
Best Regards,

Sergio B.
 
T

Tor Iver Wilhelmsen

Sergio B. said:
Ok, because you wrote "ClassLoader.getResource()" but this isn't a static
method,

No, but methods are one to a class; whether it's static or not is
something you find in the docs.
java -jar my_application
and you shouldn't be able to access the resource!

Not without adding an entry to the manifest file like

Class-path: someOther.jar yetAnother.jar
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top