Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  I have no idea how to get my images to work in a Jar!  (Read 2348 times)
0 Members and 1 Guest are viewing this topic.
Offline Silver

Senior Newbie





« Posted 2012-01-05 15:11:49 »

(I've attempted to put the tl;dr stuff in bold)
Hey everyone, I'm totally new here and relatively new to Java, I was hoping I could get some help regarding a practice game I'm making.
I did a few searches, I didn't find anything like this, but I also wasn't going to spend my entire evening tunneling through forum searches XD

Anyway, my game was working fine when I ran it in Eclipse, but I wanted to be able to show it to my buddies, I exported it as an executable Jar and ran it.
Nothing ran at all. So I looked into it and apparently my images aren't loading up properly when I run the Jar.

Why is it that Eclipse can find my images and run the thing fine and when I export it nothing works?
I'm assuming it's to do with where I placed my files. I HAVE NO IDEA WHERE TO PUT THEM! But I do want them in an images folder at the very least.
I've currently got them in "src/platformer/images"
Platformer is my package name/folder and has all my code in there.


The code I'm using to load the images up has changed a few times, but what I'm currently using is:

BufferedImage blockTileset = ImageIO.read(this.getClass().getResource("images/tiles.png"));

Now I'm not entirely sure what I'm doing there, I just read from Kev Glass' tutorials that it's a good idea to do it that way.
Can anyone lend some insight into this? Do I need to move my images folder somewhere? Do I need to add a slash or something to my file path? I've spent all day on this annoying problem and haven't got anywhere! I know it's a simple, stupid problem, but please bear with me!

Thanks in advance Smiley

P.S. You may need to dumb down your answers a little bit, I'm not a full on programmer yet! So many things I've looked up today where people had problems that were solved thusly, 'Simply do this! *insert a whole mess of uncommented code with poorly named variables*' 'Wow thanks for solving my problem! Cheesy'. Then you have me sitting here, thinking in angry scribbles and reaching for another coffee...
Offline Damocles
« Reply #1 - Posted 2012-01-05 15:17:17 »

can you write the filestructure of your jar?
what folders are in there, and where did you put the ressources

Offline Silver

Senior Newbie





« Reply #2 - Posted 2012-01-05 15:23:01 »

Sure thingy! It's a really small, simple project.

I opened the Jar in 7zip and got the following:

platformer >  images           >    All my images
               |  All my code 
       
META-INF > manifest.mf

I hope that's readable enough! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #3 - Posted 2012-01-05 15:27:32 »

try

BufferedImage img= ImageIO.read(new File("../images/clown.png"));

or

BufferedImage img= ImageIO.read(new File("../platformer/images/clown.png"));


the ../  means you go up 1 folder


Offline Silver

Senior Newbie





« Reply #4 - Posted 2012-01-05 15:32:21 »

I've tried that before Sad I'm afraid it doesn't work. Eclipse doesn't even find the images when I do it like that. The Jar still falls over too.
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2012-01-05 15:34:01 »

BufferedImage img= ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("images/clown.png"));

where images/clown.png is in your jar, just like that.

Cas Smiley

Offline Silver

Senior Newbie





« Reply #6 - Posted 2012-01-05 15:43:59 »

Sorry! That didn't work either, I tried that code with "images/", "/images/" and "../images/", still no good.
I'll go ahead and paste in the class I'm using to hold my sprites, minus the getters.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
public class Sprites 
{
   public BufferedImage[] sprites = new BufferedImage[4];
   private int tileSize = 32;
   
   public Sprites()
   {
      try
      {
         //Block tileset
        BufferedImage blockTileset =
               ImageIO.read(this.getClass().getResource("images/tiles.png"));
         
         sprites[0] =
               ImageIO.read(this.getClass().getResource("images/dude.png"));
         sprites[1] =
               ImageIO.read(this.getClass().getResource("images/enemy.png"));
         
         //Loop through block tileset
        for (int i1 = 0; i1 < 1; i1++)
         {
            for (int i2 = 0; i2 < 2; i2++)
            {
               sprites[i2 + 2] = blockTileset.getSubimage(i2 * tileSize,
                     i1 * tileSize, tileSize, tileSize);
            }
         }
      }
      catch (Exception e)
      {
         System.out.println(e.getMessage());
      }
   }


If you're wondering about the nested for loops, I'm leaving those in for when my sprite sheet exceeds two sprites haha!

I still think it's probably to do with where I've put the images. The images folder is in the same directory as all my code, surely that would be ok?
Offline Damocles
« Reply #7 - Posted 2012-01-05 15:46:16 »

In Eclipse you must assign your resourcefolder as class-folder under properties

Offline Silver

Senior Newbie





« Reply #8 - Posted 2012-01-05 15:55:22 »

That's starting to sound like the solution to my problem!
See I only just switched over to Eclipse yesterday, I've been trying to learn it as best I can, yet a little thing like this seems to have slipped under my radar.
So the properties where I assign this as a resource folder, is that the properties from the project menu? Or is it the properties from right clicking on the folder itself? Also, whereabouts in properties? Is it build path or linked resources or something I've missed entirely?

Sorry to waste time on such a small little problem!!
Offline Damocles
« Reply #9 - Posted 2012-01-05 16:02:39 »

rightclick on the project in the list, select propertes->java_buildpath
and add the imagefolder as resourcefolder

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

Senior Newbie





« Reply #10 - Posted 2012-01-05 16:09:30 »

Well I opened the project properties, went to build path, went to the source tab, added the folder and this is what happened...
Some deal about enabling nesting? Does this mean my folders are whack?

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #11 - Posted 2012-01-05 16:22:35 »

BufferedImage img= ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("images/clown.png"));

where images/clown.png is in your jar, just like that.

Cas Smiley

I think Cas is right here, but in your current zip layout, your images were in "platformer/images".  So I believe it would be:

1  
BufferedImage img= ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("platformer/images/clown.png"));
Offline Silver

Senior Newbie





« Reply #12 - Posted 2012-01-05 16:31:55 »

I used that code you suggested, it worked in Eclipse, but after exporting is an executable Jar, still didn't work.
I seriously think something is wrong with my folders or something. Am I correct in assuming that the images folder should be a source folder in the Java Build Path in properties? Because I've tried that too, and it broke it again Sad
I'm definately doing SOMETHING wrong though, since I've looked through tons of videos today and they just breezed through it...

EDIT: Here's a picture of my little project list that might shed some light on the subject. I'm not sure that images folder is in the right place, I've moved it around a fair bit though....

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-01-05 18:02:34 »

When using Class.getResource, you must know that it is relative to the Class. The easiest way to get a resource is to put a forward slash at the beginning of the path. This signifies the root of the jar file when in a JAR file or the root of your project (the src folder). In this situation, remove the "platformer.images" from the build path. To load any file, use ImageIO.read(Class.getResourceAsStream("/platformer/images/MyFile.png"));

EDIT: There is no difference between using Class and the Thread.currentThread().getContextClassLoader() way.

EDIT2: In your project image, that white colored "package icon" means there are no files under that folder....

Offline sproingie

JGO Kernel


Medals: 202



« Reply #14 - Posted 2012-01-05 18:37:03 »

Use getClass().getResourceAsStream().  This gives you a reference relative to the current class, using its classloader, which is much more likely to do what you want.  This doesn't matter much for the average toy app, but if you ever write web apps or god forbid mess with OSGi, you'll absolutely hose yourself if you start grabbing random classloaders or try to use the boot one.
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #15 - Posted 2012-01-05 19:55:08 »

EDIT2: In your project image, that white colored "package icon" means there are no files under that folder....

It means there aren't any source files in the folder.  There could be images, .properties files, etc.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-01-05 19:56:11 »

EDIT2: In your project image, that white colored "package icon" means there are no files under that folder....

It means there aren't any source files in the folder.  There could be images, .properties files, etc.
*ra4king looks at Eclipse again* oh.....whooops...you're right Grin

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #17 - Posted 2012-01-05 19:58:04 »

Use getClass().getResourceAsStream().  This gives you a reference relative to the current class, using its classloader, which is much more likely to do what you want.  This doesn't matter much for the average toy app, but if you ever write web apps or god forbid mess with OSGi, you'll absolutely hose yourself if you start grabbing random classloaders or try to use the boot one.


It's a rite of passage to work on an "enterprise" application and bang your head against the wall tinkering with classloaders.
Offline Silver

Senior Newbie





« Reply #18 - Posted 2012-01-05 23:41:23 »

At this point, I'm not even sure what exactly a class loader is, I just read that if I do it this kind of way it's better  persecutioncomplex.
I certainly appreciate all the time you guys are putting into helping me out, I've done what I think you guys were telling me to do, it looks like this now:



However, it's not even running in Eclipse anymore... Sad
So let me get this straight, the src folder is treated as the root directory right? So surely putting the images folder in src folder would mean that the path, "/images/myfile.png" would work right? ...Cause it's not. I really think the problem is WHERE I'm putting my images folder, I simply just don't know the correct place for it.
This is such a rediculous error for me to be stuck on...
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #19 - Posted 2012-01-06 00:12:54 »

Don't add it to the build path though!

The best way is to make a "res" folder inside src:

Offline aazimon
« Reply #20 - Posted 2012-01-06 00:30:31 »

Are you getting an error when you run the last code you posted? You code is fine. The location of your images is fine.

I create a jar with a main method that build a frame and this panel. It loaded the image fine.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class TestPane extends JPanel {
    private static final long serialVersionUID = 1L;
    private BufferedImage image;

    public TestPane() {
   this.setPreferredSize(new Dimension(200, 200));
   
   try {
       image = ImageIO.read(TestPane.class.getResourceAsStream("/test/Marvin.JPG"));
   } catch (IOException e) {
       e.printStackTrace();
   }
    }
   
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(image, 10, 10, this);
    }
}
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #21 - Posted 2012-01-06 00:41:35 »

No the way he has it, it won't work doing "/images/enemy.png". Since he added that folder to the build path, he can now directly do "enemy.png".

Offline aazimon
« Reply #22 - Posted 2012-01-06 00:45:45 »

My build path includes everything under src. What am I missing here?

Test (project)
 -src
 --org
       (class files)
 --test
       (image files)
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #23 - Posted 2012-01-06 01:00:12 »

Ah but you didn't specifically right click your resources folder and click "Use as Source Folder". That's what is happening in the OP's image.

Offline Silver

Senior Newbie





« Reply #24 - Posted 2012-01-06 01:20:53 »

This is starting to come together, thanks guys, it's good to see what it's supposed to look like haha! I was getting quite confused...
In any case, I remade the images folder from scratch and made sure it wasn't in the build path or anything, just a plain folder. I put laid it out the you have yours there Ra4king. Good news! It now runs in Eclipse!
Bad news... it still won't run as an executable jar...

Here's what it looks like now:

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #25 - Posted 2012-01-06 01:24:38 »

Careful, it's case sensitive when inside a JAR Wink

Offline Silver

Senior Newbie





« Reply #26 - Posted 2012-01-06 01:34:23 »

Oh my goodness! I can't believe I did that... Clearly not enough coffee!
Well it WORKS now! Thanks a lot to everyone who took the time to help out an Eclipse newbie like me! Cheesy
...Now I can resume actually coding the thing..
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #27 - Posted 2012-01-06 01:38:00 »

Grin

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #28 - Posted 2012-01-10 15:11:58 »

I remember ra4king finding the exact same mistake in my one of my first programs haha.

Check out my website!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #29 - Posted 2012-01-10 18:10:47 »

It's case-sensitive all the time, the only platform you can get away with it when it comes to the filesystem is Windows.  If you want portability, treat everything as case-sensitive all the time.
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.

Riven (7 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!