Results 1 to 3 of 3
Thread: Read file from a jar
- 01-06-2012, 11:37 PM #1
Read file from a jar
I have created a program(with GUI) and made a jar of it, so its now a program. The gui have a background image, which I load using this code:
backgroundImage = ImageIO.read(new File("mainwindow.png"));
If I place the image outside the jar, in the same directory as the jar, the image will be loaded(this is unwanted, I want to the image in the jar and read it from there).
What do I do?
- 01-07-2012, 12:43 AM #2Moderator
- Join Date
- Jul 2010
- Rep Power
Re: Read file from a jar
Retrieve the URL to the file inside the jar by using the getResource("my file") method of the class object (you can call getClass() on an instance, or use MyClass.class syntax). Then pass the URL to ImageIO. Read the API on these methods for more information on how to specify the path of the file relative to a class in a different package.
- 01-07-2012, 02:33 AM #3
- By jrnowlan in forum New To JavaReplies: 1Last Post: 08-05-2011, 10:21 PM
- By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 11:07 AM
- By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 10:53 AM
- By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 11:28 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 10:13 AM