Results 1 to 5 of 5
Thread: Getting a picture out of a jar?
- 02-02-2011, 04:34 PM #1
Getting a picture out of a jar?
Hello, I am trying to load a picture that is in a jar into my program. The current method I am using works fine if the picture is in the same folder or package as the main program but if the program is in com.main and the picture is in com.images I can't get it to work. Does anyone know how to do this? Here is my current method of getting the image:
ImageIcon imIc = new ImageIcon(this.getClass().getResource(imageName)); this.pic = imIc.getImage();
Thanks for the help pbrockway2 :)
Last edited by Zman3359; 02-02-2011 at 09:49 PM.
- 02-02-2011, 05:51 PM #2Moderator
- Join Date
- Feb 2009
- New Zealand
- Rep Power
Are you using imIc for anything? If not, just use ImageIO.read() which also takes a URL argument.
Try using System.out.println() to print the URL returned by this.getClass().getResource(imageName) - the chances are you are looking in the wrong place for the image.
Notice that the name argument of getResource(name) can start with a / in which case the name is interpreted in absolute terms. You might find that it is conceptually easier to deal with something like
- 02-02-2011, 09:37 PM #3
Nope im only using imIc to get the image, so could I do:
Image pic = ImageIO.read("/com/images/MyImage.png");
EDIT: I anwsered my own question there :p . That doesn't work but this does:
this.pic = (Image)ImageIO.read(this.getClass().getResource("/com/images/MyImage.png"));
Last edited by Zman3359; 02-02-2011 at 09:54 PM. Reason: Left out com xD
- 02-02-2011, 11:55 PM #5
- By CiderApple in forum Java 2DReplies: 1Last Post: 12-14-2010, 10:24 PM
- By ThatPinkSock in forum New To JavaReplies: 3Last Post: 10-29-2010, 01:40 AM
- By Addez in forum New To JavaReplies: 6Last Post: 11-03-2009, 08:44 PM
- By safiya in forum NetBeansReplies: 1Last Post: 10-18-2008, 07:13 AM
- By deathnote11 in forum AWT / SwingReplies: 13Last Post: 06-03-2008, 05:46 PM