Accessing resources from a jar outside a jar

The scenario is:

I’ve got 2 jars - 1 contains all the class files (including the main executable file), the other all the images i need.

The question is:

How do I load the images from within the image jar file in the application deployed using java web start?

Thanks heaps in advance :slight_smile:


getClass().getClassLoader().getResource("myimagename.png")); 

or

getClass().getClassLoader().getResourceAsStream("mystreamabledata.dat");

Kev

Sorry, I think I was being unclear with my problem description. What I meant was:

I’ve got 2 jars - 1 jar contains all the program code (ie, .class, .java and 1 dll file), the other contains all images and sound files that I would like to use within the application, which I plan to distribute using just webstart and as seperately downloadable jar files.

The problem is:

How do I load/reference the image and sound files within the “image and sound” jar from the “program code” jar?

getClass().getClassLoader().getResource("myimagename.png"));  
 
or 
 
getClass().getClassLoader().getResourceAsStream("mystreamabledata.dat");  

I’ve tried that and simply “getClass().getResource()” which I think is an equivalent statement. Both won’t work since the images are located within a jar that is outside the program jar.

Besides that, is there a convenient way to load a library dll file that is jar’red together with the program code?

First and foremost, you need to put a ‘/’ in front of your path names. Secondly, you can put a dud class in the images and sounds JAR to tell the JVM where to look. e.g.:

public class FileLocater {}

FileLocater.class.getResource("/myimage.png");

Hope this helps! :slight_smile:

To make your code jar see the files in the sound / image jar, you can add


Class-Path: SoundImage.jar

to the manifest of the code jar. After that, I think the ‘getResource()’ will work (not tried).

As for loading dll, I would be happy to find a good way to load then from a jar… What I do now is to put the dll’s in the same dir as the game’s jar.

[quote]To make your code jar see the files in the sound / image jar, you can add


Class-Path: SoundImage.jar

to the manifest of the code jar. After that, I think the ‘getResource()’ will work (not tried).
[/quote]
Yup, that worked very well. :slight_smile:

Actually, I tried that before and it didn’t work then because of a “Classpath” typo from this article (which I referred to): http://www.rgagnon.com/javadetails/java-0319.html

Thanks for cluing me in.

It works properly without that on Windows. Under what circumstances would that statement fail without the ‘/’?