Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiple Jar's  (Read 2668 times)
0 Members and 1 Guest are viewing this topic.
Offline Slavic

Senior Newbie





« Posted 2007-07-21 12:06:17 »

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
Offline Kova

Senior Member





« Reply #1 - Posted 2007-07-21 12:22:30 »

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)
Offline Slavic

Senior Newbie





« Reply #2 - Posted 2007-07-21 12:35:25 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2007-07-21 15:06:28 »

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.
Offline Slavic

Senior Newbie





« Reply #4 - Posted 2007-07-21 22:55:32 »

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
Offline Kova

Senior Member





« Reply #5 - Posted 2007-07-21 23:08:44 »

no, it doesn't matter...

Quote
if you can edit it and add resources.jar to classpath that would do the trick.
this means you add resources.jar to classpath of main jar file!
Offline Slavic

Senior Newbie





« Reply #6 - Posted 2007-07-21 23:41:09 »

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
Offline Kova

Senior Member





« Reply #7 - Posted 2007-07-22 00:11:12 »

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 Smiley
ResourcesInJarTest.java:
1  
2  
3  
4  
5  
6  
7  
8  
public class ResourcesInJarTest {
   
    public static void main(String args[]) {
        Test test = new Test();
        System.out.println("main");
    }
   
}


Test.java:
1  
2  
3  
4  
5  
public class Test {
    public Test() {
        System.out.println("outside");
    }
}


manifest.mf:
1  
2  
3  
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
Offline Slavic

Senior Newbie





« Reply #8 - Posted 2007-07-22 00:36:48 »

Ok thanks for that,

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

anymore ideas?

Offline Kova

Senior Member





« Reply #9 - Posted 2007-07-22 00:52:33 »

always more ideas... this works:

add data dir and data.txt file in it.

change ResourcesInJarTest.java to:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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

when url is bad, it writes "null", so this works ... println your url, maybe it's good but you are not handeling it properly
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Slavic

Senior Newbie





« Reply #10 - Posted 2007-07-22 01:13:38 »

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
1  
2  
3  
4  
5  
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




Offline Kova

Senior Member





« Reply #11 - Posted 2007-07-22 08:16:14 »

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
1  
2  
3  
4  
5  
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 Smiley ... 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).
Offline Slavic

Senior Newbie





« Reply #12 - Posted 2007-07-22 08:22:31 »

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
Offline Slavic

Senior Newbie





« Reply #13 - Posted 2007-07-22 21:27:41 »

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!
Offline beowulf03809

Junior Member




We live for the code, we die for the code


« Reply #14 - Posted 2007-07-25 15:31:07 »

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

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.
Offline keldon85

Senior Member


Medals: 1



« Reply #15 - Posted 2007-07-25 16:19:20 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!