Multiple Jar's

Hi All,

I have searched the internet and I think I have read all the topics about jars on this forum but I can’t seem to find a working solution to one problem I’m having.

Ok I have my class files, some gif’s, fonts and sound files and they run fine from eclipse and fine from a jar file but if I try and put the Resources into their own jar I can’t get it to work.

Any help would be great

you need to add that resource jar to classpath then, when building main jar use manifest file and you can define what else is on the classpath

like this:

jar -cfvm viktorije.jar manifest.dat “viktorije”

and in manifest.dat :
Main-Class: viktorije.Viktorije
Class-Path: resources.jar

don’t forget new line extra at the after last argument, it is required by jar standard (or else your last line won’t matter)

Thanks,

but it didnt seem to work for me

I use the export feature in Eclipse to make jar files, .
I checked in my jar that I created and I couldnt find a manifest.dat file, is this supposed to be there?

I have a MANIFEST.MF file is this the same?

is the jar with the resources in the supposed to have a manifest file too?

learn how to make jars yourself for start…

yes you can call the file whatever you want, manifest.mf is good… if you can edit it and add resources.jar to classpath that would do the trick. Yes every jar contains manifest, without it it would be only a zip file with some data (jars are zip files…). In your export wizard there must be some place you can write main class and classpath.
Everything else you can learn at official java tutorial about making jars or some other tutorials.

does it make any difference that there is no code in the Resources.jar?
I read a bit about the class-path and it seemed to only mention with code not just files

no, it doesn’t matter…

[quote]if you can edit it and add resources.jar to classpath that would do the trick.
[/quote]
this means you add resources.jar to classpath of main jar file!

ok well I have added this to the mainfest file and all my streams return as null.

I just use at the moment:

“Resources/Graphics/image1.gif”

and open this with getSystemResourceAsStream this seems to work from the same jar

if stream works and .getResource() doesn’t on same directory structure then you resource.jar isn’t on classpath. Check what’s inside manifest file of main.jar … it should be something like Class-Path: resources.jar.
If you want to be sure if your resources.jar is on classpath for testing put some empty class into it, like Test.class and just try to make a new inst

ance of it in your main app:
Test test = new Test(); … it will fail if resources.jar isn’t on classpath.

edit: of course, add jar to class path when compileing

ok, here is complete example :slight_smile:
ResourcesInJarTest.java:

public class ResourcesInJarTest {
    
    public static void main(String args[]) {
        Test test = new Test();
        System.out.println("main");
    }
    
}

Test.java:

public class Test {
    public Test() {
        System.out.println("outside");
    }
}

manifest.mf:

Manifest-Version: 1.0
Main-Class: ResourcesInJarTest
Class-Path: /test.jar


(note extra line at the end)

compile and run:
javac Test.java
jar -cfv test.jar Test.class
javac ResourcesInJarTest.java -cp test.jar
jar -cfvm main.jar manifest.mf ResourcesInJarTest.class
java -jar main.jar

Ok thanks for that,

I got that to work but still wont read the other files

anymore ideas?

always more ideas… this works:

add data dir and data.txt file in it.

change ResourcesInJarTest.java to:

public class ResourcesInJarTest {
    
    public ResourcesInJarTest() {
        java.net.URL url = this.getClass().getClassLoader().getResource("data/data.txt");
        System.out.println(url);
    }
    
    public static void main(String args[]) {
        Test test = new Test();
        System.out.println("main");
        new ResourcesInJarTest();
    }
    
}

and when compileing test.jar also add data dir:
jar -cfv test.jar Test.class data

everything else is the same as in post above
I get output:

[quote]outside
main
jar:file:/E:/Java/test/test.jar!/data/data.txt
[/quote]
when url is bad, it writes “null”, so this works … println your url, maybe it’s good but you are not handeling it properly

ok I tried that and the test class runs and the txt file returns null,

I have:

Resources.jar which has in it:

Test.Class
data/data.txt
META-INF/MANIFEST.MF

MANIFEST.MFcontains:
Manifest-Version: 1.0

Test.class contains


public class Test{
	public Test(){
		System.out.println("Running test class");
	}
}

and Testing.jar

ResourcesInJarTest.class
META-INF/MANIFEST.MF

MANIFEST.MFcontains:
Manifest-Version: 1.0
Main-Class: ResourcesInJarTest
Class-Path: resources.jar

and I have used your code for ResourcesInJarTest.class

are your on linux? In windows, there is no difference between Resources.jar and resources.jar, but I don’t know for linux… change names so they match

What output do you get? NoClassDefFoundError? If this is true then resources.jar isn’t on classpath, put some new lines after resources.jar in manifest and try again, with case-sensitive just in case.

If you get “outside/main/null” then your path to data.txt is wrong :slight_smile: … show me the code for path (for example, use “/” instead of “”).
Hmm… I just realized you said you used my code, “data/data.txt”, so I don’t know what is wrong. Try without the directory data, just data.txt (both in jar and in path of course).

Im on windows but I would like it to work on linux also.

its on my other computer but I think I get

Main
Running test class
null

its definitely a path issue but it just doesnt make any sence, I will try the file in the root of the jar and see if it can find it

Edit: ok well that workied in the root so the result is:

Running test class
main
jar:file:/C:/Documents%20and%20Settings/Doug/Desktop/COR/resources.jar!/data.txt

I will make the folder again and see if its just that its not liking the folders

Edit again ok well I put it in the other folder and that seems to have worked ok I will try my main game again

Thanks Kova not sure what I was doing wrong but it seems to be working now.

ok now what I have learned is that reading files from another jar is the same as the one jar the only thing is that you need to add the other jar names to the class path in the mainfest of the main jar.

I can’t see why it didnt work in the first place because it Im sure its the same but anyways thanks!

Any Unix-based system is going to be case sensitive in all path and resource names. It’s a good habit to take this into account even when working on a 'doze environment.

Here’s how I did it:

  • in Eclipse I go to the build path and add the Jar file as a library
  • add Class-Path: resource.jar to the manifest file in the launching Jar file

This allows you to use getResource() for both (a) your exported Jar and (b) when running in your Eclipse project.