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  
  Image excluded when exporting into a jar?  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS
« Posted 2013-11-09 00:26:03 »

My java project looks like this:
FirstGame
   src
      (default package)
      0.png

(The code is stored in default package.)

I load my images:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public static void loadImages() {
      for (byte i = 0; i < 1; i++) {
         try {
            image[i] = ImageIO.read(new File("src/" + i + ".png"));
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }

(No errors are given when compiling and I can display the image in the game.)

When I export the project into a runnable jar file it seems as if the image doesn't exist. I can extract the jar's contents with 7-zip and I can clearly see my image is there however.

So the question is, how do I make sure the image is included and in the right place when I export it?

Offline Dxu1994
« Reply #1 - Posted 2013-11-09 00:30:05 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
public static void loadImages() {
      for (byte i = 0; i < 1; i++) {
         try {
            image[i] = ImageIO.read(new File("src/" + i + ".png"));
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }


Replace your code with this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public static void loadImages() {
      for (byte i = 0; i < 1; i++) {
         try {
            image[i] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("/src/" + i + ".png"));
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }


Why your code doesnt work: You cant access a file inside a JAR file with File. You need the input stream.

Offline VaPS
« Reply #2 - Posted 2013-11-09 01:00:41 »

Replace your code with this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public static void loadImages() {
      for (byte i = 0; i < 1; i++) {
         try {
            image[i] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("/src/" + i + ".png"));
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }


Why your code doesnt work: You cant access a file inside a JAR file with File. You need the input stream.

I replaced the code and got an error message regarding static context. After editing the function to non-static in your version of the code I got this error upon compiling:
1  
2  
3  
4  
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
   at javax.imageio.ImageIO.read(Unknown Source)
   at Background.loadImages(Background.java:16)
   at Game.main(Game.java:45)

Background.java:16:
1  
2  
image[i] = ImageIO.read(getClass().getClassLoader()
                  .getResourceAsStream(i + ".png"));

Game.java:45:
1  
new Background().loadImages();


The last piece of code is executed when launching the project. I normally would use the entire class Background in a static context and I suspect that might be the problem.
Does anybody have any idea?

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

Junior Member


Medals: 4



« Reply #3 - Posted 2013-11-09 01:18:52 »

Seems way too simple and trivial but did you try removing the "/" before "src"?
1  
image[i] = ImageIO.read(Foo.class.getClassLoader().getResourceAsStream("/src/" + i + ".png"));

to
1  
image[i] = ImageIO.read(Foo.class.getClassLoader().getResourceAsStream("src/" + i + ".png"));
Offline VaPS
« Reply #4 - Posted 2013-11-09 01:23:08 »

Seems way too simple and trivial but did you try removing the "/" before "src"?
1  
image[i] = ImageIO.read(Foo.class.getClassLoader().getResourceAsStream("/src/" + i + ".png"));

to
1  
image[i] = ImageIO.read(Foo.class.getClassLoader().getResourceAsStream("src/" + i + ".png"));

I already tried that with no luck  Undecided

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-11-09 01:42:22 »

Your problem is your class is in the source folder already, so you don't need to reference it when loading the image. Try removing the "src/" completely when loading the image.

Offline VaPS
« Reply #6 - Posted 2013-11-09 13:29:08 »

Your problem is your class is in the source folder already, so you don't need to reference it when loading the image. Try removing the "src/" completely when loading the image.
I edited my code like your suggestion yesterday without trying it out because I was tired and thought it was going to do nothing.
I just tried it out and everything works perfectly! Thanks for the help everyone!

Edit: Follow-up question out of curiosity: What use is there in using my original method of grabbing images since it doesn't work after exporting?

Offline SHC
« Reply #7 - Posted 2013-11-09 13:38:50 »

What use is there in using my original method of grabbing images since it doesn't work after exporting?

Your former method doesn't work when inside JAR because
File
isn't supported inside JAR files. It can be useful if the resources were placed outside the JAR file.

Offline VaPS
« Reply #8 - Posted 2013-11-09 13:40:18 »

What use is there in using my original method of grabbing images since it doesn't work after exporting?

Your former method doesn't work when inside JAR because
File
isn't supported inside JAR files. It can be useful if the resources were placed outside the JAR file.
So I can export a jar file and have my folder of images in the same directory, and the jar could grab those files?

Offline SHC
« Reply #9 - Posted 2013-11-09 13:40:44 »

Yes for sure.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VaPS
« Reply #10 - Posted 2013-11-09 13:41:57 »

Yes for sure.
Thanks a bunch! Going back to actually making progress on my game!

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