Java Technology Forums
Skip Masthead Links developers.sun.com   » search tips  |  Search:     
The Source for Developers
Products and TechnologiesTechnical Topics
Sun Microsystems
 
 
Java Technology Forums
Topic: Reading a Text File from a Jar

 
You are not signed in.
 
Search Forum
 

Previous Topic   Next Topic

This topic has 7 replies on 1 page (Most recent message: Jan 11, 2004 3:37 PM)
Reading a Text File from a Jar
Author: michaeltaft
Jan 11, 2004 3:37 AM  
I've been trying to make this work all day. Something is wrong. I'm building a nice little installer for a program, and want to display the GNU license in a JTextArea. I have the GNULicense as a .txt file in the "doc" subdirectory in the Jar archive. I want to read the license text into the JTextArea for display.

Here is the relevant code:\


readin("doc/GNULicense.txt");

....

public void readin(String s) {
String thisLine;
try {
InputStream is = getClass().getResourceAsStream(s);
BufferedReader br = new BufferedReader
(new InputStreamReader(is));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);//this is just for testing - actually I'll make this go into the JTextArea
}
}
catch (Exception e) {
e.printStackTrace();
}
}

I've also tried the InputStream line as: InputStream is = this.getClass().getResourceAsStream(s);

Either way I get a null pointer exception. I've explored the Jar ad nauseum, and everything is in the right place with the right names. What is going on here? I'm totally confused and frustrated.
Thanks for any help.
M.

Re: Reading a Text File from a Jar
Author: hiwa
In Reply To: Reading a Text File from a Jar
Jan 11, 2004 4:49 AM  
Reply 2 of 7
> readin("doc/GNULicense.txt");
This means doc subdirectory belongs to the current directory where your program runs.If there's no doc subdir, and GNULicense.txt under it, in your current dir, your program won't work. There's a special protocol for the argument of Class#getResourceAsStream() method. Read the javadoc for the method. My vague memory tells:

readin("/doc/GNULicense.txt"); //the right code for Class#getResourceAsStream() method argument in a jar-archived program and resource files

Re: Reading a Text File from a Jar
Author: michaeltaft
In Reply To: Re: Reading a Text File from a Jar
Jan 11, 2004 3:36 PM  
Reply 6 of 7
This extraordinarily simple (4 keystrokes) solution worked first try. Many thanks, and two duke dollars.

I appreciate everybody's help.

M.

Sorry, I mean to quote the original, which was changing the line to

"/doc/GNULicense.txt"

and actually it took ONE keystroke. Muchas gracias.

 

Powered by Jive Forums NOTICE TO USERS OF THIS SITE: By continuing to use this site, you certify that you have read and agree to abide by Sun's "Terms Of Use" which are linked to this site at the bottom of the page. Any users of this site who state that they work for Sun Microsystems, Inc. are deemed to be doing so solely for identification purposes. All information, data, text, software, music, sound, photographs, graphics, video, messages, or other materials ("Content") posted to this site by any users are the sole responsibility of those users. Sun does not guarantee the accuracy, integrity, or quality of such Content. Posting a name in a discussion forum places that personal information into a public forum.