Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  How to load an image from a jar? [solved]  (Read 524 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2014-07-17 13:52:42 »

my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   public static BufferedImage grabImage(String path, int x, int y, int width, int height) {
      BufferedImage tile = null;
      BufferedImage bildTileset = null;
      try {
         bildTileset = ImageIO.read(new File(path));
      } catch (IOException error) {
         System.err.println("Tileset not found");
         error.printStackTrace();
      }
      tile = bildTileset.getSubimage(x * width, y * height, width, height);
      return tile;
   }

//exapmple
// BufferedImage img = grabImage("res/img/img.png",0,0,16,16);

But it wont work! It loads if it is outside of the jar! How do i load it form inside the jar?

Java freak! Cheesy
Online BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #1 - Posted 2014-07-17 13:56:29 »

http://www.java-gaming.org/topics/faq-cannot-find-the-resource-file/33226/view.html
Offline KevinWorkman

JGO Kernel


Medals: 116
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #2 - Posted 2014-07-17 14:52:12 »

In addition to what BurntPizza posted, I'll offer my own explanation:

Stuff inside of jars aren't files anymore. They're resources. So the File class will no longer work with stuff inside a jar- because they aren't files. They're resources.

Instead, you need to access them as resources. You do this using the handy getResource() and getResourceAsStream() methods found in ClassLoader (use this if your resources are relative to your whole jar) and Class (use this if your resources are relative to your class).

For example, your problematic line might look like this instead:

1  
ImageIO.read(YourClassName.getClass().getClassLoader().getResourceAsStream(path));


More info here: http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html
Or in the API: http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-
Or just google "java load resources" for a ton of results.

Edit: I should also note that the getResource() methods will also work with files outside your jar. So it's perfectly reasonable to use these methods from your IDE as well.

Static Void Games - Play indie games, learn game programming, upload your own games!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #3 - Posted 2014-07-17 14:55:57 »

Indeed, although "resources" is a bit hand-wavey IMO, they are technically zip-file entries, as jar files are actually thinly veiled zip archives.
Offline KevinWorkman

JGO Kernel


Medals: 116
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #4 - Posted 2014-07-17 14:59:39 »

Indeed, although "resources" is a bit hand-wavey IMO, they are technically zip-file entries, as jar files are actually thinly veiled zip archives.

Correct. Either way, the File class doesn't work with them, and the getResource() methods must be used instead.

Static Void Games - Play indie games, learn game programming, upload your own games!
Online BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #5 - Posted 2014-07-17 15:05:10 »

I believe that one can also use a JarInputStream or JarFile to read the currently executing jar as if it was a directory structure as well, although certainly getResource() is much more convenient. Just pointing out that there's nothing magical about the "currently executing jar," it's a readable file like any other.

But yeah, just use getResourceAsStream() and use Riven's tool I linked if you have trouble.
Offline KevinWorkman

JGO Kernel


Medals: 116
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #6 - Posted 2014-07-17 15:11:50 »

I believe that one can also use a JarInputStream or JarFile to read the currently executing jar as if it was a directory structure as well, although certainly getResource() is much more convenient.

You can use JarFile and JarInputStream (and JarEntry) to view the contents of a jar, but to view the contents of the contents I'm pretty sure you'd have to extract them to the file system outside the jar first.

In other words, you can use the Jar* classes to see that a Jar contains a .txt file, but to actually view that .txt file, you'd have to extract it first. Or just use the getResource() methods.

Static Void Games - Play indie games, learn game programming, upload your own games!
Online BurntPizza

« JGO Bitwise Duke »


Medals: 298
Exp: 5 years



« Reply #7 - Posted 2014-07-17 15:17:56 »

You could do that, or read entries piecemeal via JarFile.getInputStream(<entry>) if I'm not mistaken.
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 849
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2014-07-17 17:43:57 »

There is no need to store archive entry contents to disk to read them, just like there is no need to store socket data to disk to read it. Just read bytes from the stream.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (12 views)
2014-12-27 04:03:04

TheDudeFromCI (16 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (96 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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