Results 1 to 3 of 3
Thread: Read file from a jar
- 01-06-2012, 10: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:
Placing the image in the root of the jar file give me run time error(cannot load image).Java 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-06-2012, 11:43 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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, 01:33 AM #3
Similar Threads
-
Read CSV File, sort records, and output a new csv file
By jrnowlan in forum New To JavaReplies: 1Last Post: 08-05-2011, 09:21 PM -
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks