Results 1 to 14 of 14
Thread: Images in JAR File (via Eclipse)
- 10-07-2009, 04:22 AM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Images in JAR File (via Eclipse)
Hey, I'm using Eclipse. I've written the first portion of my latest program, which uses images in a source folder called "img". When I run it in Eclipse, the images show up fine. However, when I try to execute the JAR file they do not show up. Perhaps I'm having trouble understanding exactly how I'm suppose to export them.
I searched for help on sun's website ( New To Java - Re: How can I pack images into a runnable jar file in Eclipse? ), and tried to follow the help provided there. However, nothing I did seemed to work. Can someone try to explain what I need to do? Help appreciated.
- 10-07-2009, 07:35 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Make sure that that directory actaully exists in the Jar and ensure that you are using getResource properly (and you won't be able to "File" in any way shape or form).
Edit: It would help if you posted the code used to "load" the images.
- 10-07-2009, 04:07 PM #3
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
I don't know anything about getResource, so I assume that's my problem.
Java Code://Example Image use import javax.swing.ImageIcon; public class Grass extends AreaComponent { public Grass() { super( new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), false); } }
Would this be the proper way to go about doing this?
Java Code://Example Image use import javax.swing.ImageIcon; public class Grass extends AreaComponent { public Grass() { super( new ImageIcon(getResource("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon(getResource("img\\AreaComponent\\Grass\\1.gif"))).getImage(), false); } }Last edited by AndrewM16921; 10-07-2009 at 04:23 PM.
- 10-07-2009, 04:24 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 10-07-2009, 04:35 PM #5
To build upon what masijade mentioned about "getResource":
To understand where things are to your existing file path:
Java Code:File file = new File(""); //this is currently where you are looking for your grass System.out.println("[not going to find it here]: " + file.getAbsolutePath()); System.out.println("[were are getting closer]: " + this.getClass().getResource("")); System.out.println("[perhaps here (is it not null)?]: " + this.getClass().getResource("img/AreaComponent/Grass/1.gif")); System.out.println("[perhaps here (is it not null)?]: " + this.getClass().getResource("../img/AreaComponent/Grass/1.gif")); //When you find the something that is non-null then: ImageIcon foundIt = new ImageIcon(this.getClass().getResource("img/AreaComponent/Grass/1.gif"));My Hobby Project: LegacyClone
- 10-07-2009, 08:13 PM #6
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
I'm still immensely confused. For your code example, I kept getting null, even after I messed with it a bit based on the sun tutorial. Maybe if I post two of my classes (AreaComponent and Grass), someone can try to explain how to go about using getResource() properly. I've tried like 6 different implementations, each of which either gives me an error or a nullPointerException.
Java Code:import javax.swing.ImageIcon; public class Grass extends AreaComponent { public Grass() { super(new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), false); } }Java Code:import java.awt.Image; public class AreaComponent { private Image occupiedImage, unoccupiedImage; private boolean occupied, solid; public AreaComponent(Image occupiedImg, Image unoccupiedImg, boolean sol) { occupiedImage = occupiedImg; unoccupiedImage = unoccupiedImg; solid = sol; } /* Run this method if the player enters this space */ public void playerEnter(Area area, Player player) { occupied = true; } /* Run this method if the player exits this space */ public void playerExit(Area area, Player player) { occupied = false; } /* Returns altered Area when player enters */ public Area areaActionPlayerEnter(Area area, Player player) { return area; } /* Returns altered Area when player exits */ public Area areaActionPlayerExit(Area area, Player player) { return area; } /* Sets occupied to occupy */ public void setOccupied(boolean occupy) { occupied = occupy; } /* Returns the appropriate Image */ public Image getImage() { if(!occupied) return unoccupiedImage; else return occupiedImage; } /* Returns true if the AreaComponent is currently occupied, false otherwise */ public boolean isOccupied() { return occupied; } /* Returns true if the AreaComponent is solid, false otherwise */ public boolean isSolid() { return solid; } }
- 10-07-2009, 09:10 PM #7
What is displayed when you run this code?
Java Code:import javax.swing.ImageIcon; public class Grass extends AreaComponent { public Grass() { //super(new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), false); super(null, null, false); javax.swing.JOptionPane.showMessageDialog(null, "" + this.getClass().getResource("")); } }My Hobby Project: LegacyClone
- 10-07-2009, 10:39 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Have you bothered reading that tutorial yet (probably not), there is a section dedicated to using getResource for loading the images. I don't post things for no reason.
- 10-07-2009, 11:34 PM #9
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Yes, I did look at the tutorial. I didn't seem to understand it entirely, but I will look again.
It prints:
file:/F:/Workspace/DarkBrotherhood/bin/
[hr]
Now, I'm thinking something like
Grass.class.getResource("img\\AreaComponent\\Grass \\1.gif")
But I'm not sure exactly how to implement this.Last edited by AndrewM16921; 10-07-2009 at 11:39 PM.
- 10-08-2009, 12:47 AM #10
- 10-08-2009, 03:33 AM #11
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
I actually figured out a way to do it without using getResource. Thanks for the help, anyway.
-
- 10-08-2009, 07:32 AM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
P.S. With getResource you use "/" not "\\", and, now, look at the following situation
Your jarfile has the following setup
now from YourClass you use getResource as follows:Java Code:package1 package2 YourClass.class img yourimage.gif
getResource("img/yourimage.gif")
now suppose your jarfile is setup as follows
Now getResource is as follows:Java Code:package1 package2 YourClass.class img yourimage.gif
getResource("/img/yourimage.gif")
Do you see the difference in those? A leading "/" on the path in getResource means "start from the package root". If a leading "/" is not present it means "start from the current package".
Do you understand?
- 10-08-2009, 06:31 PM #14
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Similar Threads
-
code run with eclipse but not with bat file
By livnihai in forum EclipseReplies: 15Last Post: 09-17-2009, 03:58 PM -
jar file eclipse
By vishnu5 in forum EclipseReplies: 5Last Post: 09-08-2009, 08:15 PM -
Eclipse Bug - Can't Read From A File Using Eclipse?
By carlodelmundo in forum New To JavaReplies: 6Last Post: 01-26-2009, 04:25 PM -
jsp file in Eclipse
By nagaprasanna in forum EclipseReplies: 0Last Post: 11-18-2008, 08:21 AM -
eclipse batch file
By doug99 in forum EclipseReplies: 6Last Post: 04-24-2008, 02:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks