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 (408)
games submitted by our members
Games in WIP (293)
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  
  Problems while loading resources form a jar file  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Offline Zig-Zag

Senior Newbie





« Posted 2007-02-28 21:46:57 »

Hi,

I intended to post about a porblem with my litte Tileengine but when putting a testcase together i got an other problem while making a nice webstart for you.

I'm using Eclipse for development and load my Images (located in a sub folder "res/") like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public BufferedImage loadImage(String ref) {
...
  BufferedImage source = null, target = null;
  java.io.File url = new java.io.File(ref);
  try {
    source = ImageIO.read( url );
  }
  catch( IOException ioe )  {
    System.err.print( "Error while loading '" + ref + "': " );
    ioe.printStackTrace();
  }
...
}

where String ref is something like "res/box.png"

java.io.File url = new java.io.File(ref); won't work out for jars so after searching the web a bit I tried:
java.net.URL url = ((java.net.URLClassLoader)ResourceLoader.class.getClassLoader()).findResource( ref );
java.net.URL url = this.getClass().getClassLoader().getResource(ref);
java.net.URL url = Thread.currentThread().getContextClassLoader().getResource(ref);

non of them will work neither directly in eclips nor exported as jar file and I don't have any ideas left.
Maybe you can give me a hint in the right direction.

The full testcase (including Eclipse project files) can be found here (38kb jar file which will not run of course the problems mentioned above).



Offline Kova

Senior Member





« Reply #1 - Posted 2007-02-28 23:45:31 »

You can use ImageIO.read(), that surely works... I would gave you link to method I created for loading and creating compatible images, but it has misteriusly disappeared from Shared Code section. Here it is directly, for your purposes you'll need to modify class stuff and/or remove static :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   /** load image from PATH and make it compatible so it can be accelerated
     * @author Kova
     * @param path - relative path to image, ex.: "images/some_image.png"
     * @param image_type - type of BufferedImage
     * @return compatible loaded BufferedImage or null if reading fails
     * for now it's fail-fast and uses System.exit() on failure to load an image
     * @see Transparency.OPAQUE, .BITMASK, .TRANSLUCENT
     */

   public static BufferedImage loadAndCreateCompatibleImage(String path, int image_type) {
      URL url = null;
       try {
          url = Viktorije.class.getClassLoader().getResource(path);
            GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
            BufferedImage temp1_bfimg = ImageIO.read(url);
            BufferedImage temp2_bfimg = gc.createCompatibleImage(temp1_bfimg.getWidth(), temp1_bfimg.getHeight(), image_type);
            temp2_bfimg.getGraphics().drawImage(temp1_bfimg, 0, 0, null);
           return temp2_bfimg;
       } catch (Exception e) {
          System.out.println("Error loading image: " + path + " " + url);
          System.exit(0);
          return null;
       }
   }

Offline cylab

JGO Knight


Medals: 30



« Reply #2 - Posted 2007-02-28 23:56:52 »

just use
1  
URL url= this.getClass().getResource(res);


and make sure you start your res-path with a '/', e.g. "/res/image.png". This loads the resource from the current classpath, so in eclipse, your "res" folder has to be copied to the compiled classes or somewhere beneath your classpath. In your jar, the "res" folder has to be a toplevel folder in the archive.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Kova

Senior Member





« Reply #3 - Posted 2007-03-01 01:45:18 »

just use
1  
URL url= this.getClass().getResource(res);


and make sure you start your res-path with a '/', e.g. "/res/image.png". This loads the resource from the current classpath, so in eclipse, your "res" folder has to be copied to the compiled classes or somewhere beneath your classpath. In your jar, the "res" folder has to be a toplevel folder in the archive.


? without leading '/' it works just fine, in eclipse and in jar
Offline Zig-Zag

Senior Newbie





« Reply #4 - Posted 2007-03-01 02:49:12 »

Thank you both.

URL url= this.getClass().getResource(res); will do fine for me w/ leading '/'.
ResourceLoader.class.getClassLoader().getResource(path); will do fine for me w/o leading '/'.

Offline cylab

JGO Knight


Medals: 30



« Reply #5 - Posted 2007-03-01 13:47:12 »

? without leading '/' it works just fine, in eclipse and in jar
Only if you compile into the default package, since resources are loaded relative (kind of):
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getResource(java.lang.String)

Mathias - I Know What [you] Did Last Summer!
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 (101 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

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

UnluckyDevil (205 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.15 seconds with 20 queries.