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.
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?
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
[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!
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
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");
}
}
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
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 … 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).
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.