Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 1125 times)
0 Members and 1 Guest are viewing this topic.
Offline Zig-Zag

Senior Newbie





« Posted 2007-02-28 20: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 22: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 Ninja


Medals: 43



« Reply #2 - Posted 2007-02-28 22: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!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2007-03-01 00: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 01: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 Ninja


Medals: 43



« Reply #5 - Posted 2007-03-01 12: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.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (11 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
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!