Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
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  
  Accessing resources from a jar outside a jar  (Read 1128 times)
0 Members and 1 Guest are viewing this topic.
Offline barfy

Junior Member




The evidence of things not seen


« Posted 2004-08-10 17:25:47 »

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 Smiley
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #1 - Posted 2004-08-10 18:40:25 »

1  
2  
3  
4  
5  
getClass().getClassLoader().getResource("myimagename.png")); 

or

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


Kev


Offline barfy

Junior Member




The evidence of things not seen


« Reply #2 - Posted 2004-08-11 02:55:53 »

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?


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




Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2004-08-11 03:05:46 »

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.:
1  
public class FileLocater {}

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


Hope this helps! :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline gangrel-br

Junior Member




Java and Scala! Thats the game =)


« Reply #4 - Posted 2004-08-11 03:10:24 »

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

1  
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.

Paulo "JCranky" Siqueira
Offline barfy

Junior Member




The evidence of things not seen


« Reply #5 - Posted 2004-08-11 04:30:54 »

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

1  
Class-Path: SoundImage.jar


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


Yup, that worked very well.  Smiley

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.

Quote

First and foremost, you need to put a '/' in front of your path names.


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

Quote

Secondly, you can put a dud class in the images and sounds JAR to tell the JVM where to look. e.g.:
1  
public class FileLocater {} 


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



Hope this helps!  



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

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (64 views)
2013-05-17 21:29:12

alaslipknot (73 views)
2013-05-16 21:24:48

gouessej (104 views)
2013-05-16 00:53:38

gouessej (101 views)
2013-05-16 00:17:58

theagentd (111 views)
2013-05-15 15:01:13

theagentd (101 views)
2013-05-15 15:00:54

StreetDoggy (146 views)
2013-05-14 15:56:26

kutucuk (169 views)
2013-05-12 17:10:36

kutucuk (168 views)
2013-05-12 15:36:09

UnluckyDevil (177 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.102 seconds with 20 queries.