7 Replies - 3398 Views - Last Post: 24 March 2012 - 11:19 PM Rate Topic: -----

#1 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 07:07 PM

I am working on a Mancala application using Eclipse. The parts I have finished so far work fine in Eclipse. When I export them into a .JAR I'm getting weird results no matter what I try.

Firstly, I am loading my images into an ArrayList like this:
randomGem.add(0,ImageIO.read(new File("res/RedGem.png")));  //randomGem is an ArrayList<BufferedImage>


Basically, the gem images are loaded into JPanels and then are placed on a board image that is in a JPanel. The final board/gems JPanel is loaded into my JFrame window.

My folder structure is like so:
workspace/Mancala/src/mancala/*.java
workspace/Mancala/res/*.png


On searching for an answer, many people said to make my "res" folder a source folder inside of eclipse. So I have done that(right click->Build Path->Make Source).

My application runs fine in Eclipse at this point.

After creating the .JAR file, its file structure is like so:
mancala.jar/*.png
mancala.jar/mancala/*.class


On dclicking the .JAR, it opens a blank window w/ no images and sort've hangs there. I have the impression that it's throwing the exception about not finding the file.

Even if I move the .JAR into a folder that contains the images, still does not work.

*****************************************
Next Attempt:
I removed the res/ in:
randomGem.add(0,ImageIO.read(new File("res/RedGem.png")));


to
randomGem.add(0,ImageIO.read(new File("RedGem.png")));



The application and the .JAR both pop up a blank window.
However if I move the .JAR into a folder containing all of the *.png, the .JAR runs perfectly.

I'm starting to get burnt out on trying to figure this out.
I've tried a few other things that I can't even remember, since it's been quite a few hours.

I have created many, many .JAR files, but never any using images in a separate folder. My manifest file is correct, and I am also compiling using the 1.6 compliance level.

What am I doing wrong?
How do people normally set up resource and source directories? Obviously it looks messy to have everything in just one root directory, so there must be a solution I haven't figured out?

Is This A Good Question/Topic? 0
  • +

Replies To: Eclipse, Building .JAR, res/src folders

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 08:13 PM

This is what I do. I right click on my project name and go New -> Folder. I name is libs, then subdir images, sounds, etc. I put all my assets in the libs folder, then when I export to runnable Jar, I simply copy my libs folder to wherever I saved my jar. No problems. Works every time.
Was This Post Helpful? 0
  • +
  • -

#3 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 08:18 PM

I can get it to do what you are saying farrell. However, I was trying to figure out how you make the .JAR with all of the resources included and not having to do any external moving around. Create it and it works wherever I create it.

Is that not possible then?

I know my OP is very convoluted, but I do not know really what I'm needing to do.

Edit: Another thing Farrell. Aren't you duplicating all of your 'assets' by doing it that way?

This post has been edited by Achri: 24 March 2012 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 08:22 PM

View PostAchri, on 25 March 2012 - 03:18 AM, said:

I can get it to do what you are saying farrell. However, I was trying to figure out how you make the .JAR with all of the resources included and not having to do any external moving around. Create it and it works wherever I create it.

Is that not possible then?

I know my OP is very convoluted, but I do not know really what I'm needing to do.

Edit: Another thing Farrell. Aren't you duplicating all of your 'assets' by doing it that way?



You can have it budle all of your required assets into the JAR. Right-click on project then go to 'Export', then runnable Jar,. On that screen you will be able to specify whether or not to bundle everything into the jar.

You are not duplicating your assets based on my original reply. Good luck!
Was This Post Helpful? 0
  • +
  • -

#5 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 08:28 PM

I've been doing this:

right click project->Export->JAR File

Then after hitting next, I select all my resources.

Is there another way you are talking about then?

My created .JARs always have my resources in them. It just doesn't load them. Even if I have them in their own folder, or I have them declared as "source" so that they are all dumped out in the .JAR root file.
It won't load them on trying to run the .JAR.


Is there something wrong with using this:

boardImage = ImageIO.read(new File("Board.png"));


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 10:50 PM

You can't just read, as if it was from a file, a .png (or actually .whatever) from a jar
You'll have to use:

ImageIcon imgIcon = new ImageIcon(getClass().getResource("xxxxxx/redGem.png"));
where the xxxxxx is the relative path from your .class in the .jar

Then you can get the Image from the ImageIcon

As far as creating the JAR itself do it by hand. Add this one to your Favorites

http://docs.oracle.c...asicsindex.html
Was This Post Helpful? 1
  • +
  • -

#7 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 11:09 PM

View Postpbl, on 24 March 2012 - 10:50 PM, said:

You can't just read, as if it was from a file, a .png (or actually .whatever) from a jar
You'll have to use:

ImageIcon imgIcon = new ImageIcon(getClass().getResource("xxxxxx/redGem.png"));
where the xxxxxx is the relative path from your .class in the .jar

Then you can get the Image from the ImageIcon

As far as creating the JAR itself do it by hand. Add this one to your Favorites

http://docs.oracle.c...asicsindex.html


haha, I was hoping you would reply. I was at my wits end.

I'm not quite sure I understand why what I was doing wouldn't work in a .JAR, but does work from my IDE?
When would you use the ImageIO static classes, then?

And I've always known how to manually create .JARs, I just thought Eclipse could do it well enough. But, I will start getting in the habit of manually processing .JARs.

Thanks for the help.

Edit: This is where I received my information
http://docs.oracle.c.../loadimage.html

This post has been edited by Achri: 24 March 2012 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Eclipse, Building .JAR, res/src folders

Posted 24 March 2012 - 11:19 PM

The way you do it will work when reading from the file system. As soon as you put it on a .ar you are reading from the .jar and this is not a file system... unless your images are not in the .jar and you still want to read them from the file system. In that case, you will have to deploy bot the .jar and the .png that go with it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1