Reading an included data (txt) file when JAR is executed?

This seems to be a pretty popular question. I've gone through the posts here all the way back to March though and am unable to find a suitable answer. I also checked through most of Sun's Java tutorials on the website and can not find any references to this particular issue with JAR files.

I have a program that reads a text file, parses it into a Vector to be used for searches. When I want to compress the program into a JAR file and execute that JAR (by double clicking on it), the program will load but gives me an error that it can't find the text file.

My program uses that text file in the following code:

FileReader fr = new FileReader ("card_library_all.txt") ;

BufferedReader bin = new BufferedReader (fr) ;

When I run the program using the JAR file, the program reports that card_library_all.txt is not found. It will run ok if I extract the text file out of the JAR and into the same directory with the JAR.

Now, I've seen references to add a getResource() method and something about creating a URL object, but I'm not sure how to do this and properly incorporate it into my program so that it points to the location of the text file (in the JAR) as well as continue to properly use FileReader as its intended and original function. Also, if someone were to uncompress the JAR file and store all of the files to a folder and try to run it that way, would the getResource() or URL reference interfere with locating the now-extracted txt file?

I know I have lots of questions, does anyone have lots of answers? Thanks.

Cheers,

the sherlock

[1632 byte] By [sherlockazulu] at [2007-9-26 2:41:42]
# 1

Try the foll code.I have created a small application for u.

This 'd work.U can put ur text file in the jar and pls take care that if u r saving ur file in a directory then u 'd folow that directory structure while referring the resource names

This is working fine for me

GO FOR IT!!!!!!!!!!!!!

Pramod

SOURCE CODE

============

package testtest;

/**

* Insert the type's description here.

* Creation date: (8/2/01 11:48:12 AM)

* @author: Administrator

*/

public class Test {

/**

* Test constructor comment.

*/

public Test() {

super();

}

/**

* Insert the method's description here.

* Creation date: (8/2/01 11:50:26 AM)

* @param args java.lang.String[]

*/

public static void main(String[] args) {

try{

Test ts = new Test();

System.out.println();

String str = (ts.getClass().getResource("/test.txt")).getFile();

java.io.File fil = new java.io.File(str);

java.io.FileReader fr = new java.io.FileReader (fil);

java.io.BufferedReader bin = new java.io.BufferedReader (fr) ;

System.out.println(bin.readLine());

}

catch(Exception e){

System.out.println(e.toString());

}

}

}

Pramod

pramod_bs at 2007-6-29 10:17:43 > top of Java-index,Desktop,Deploying...
# 2

Hi again

If u r having problem with the code I have already posted please try this

package testtest;

/**

* Insert the type's description here.

* Creation date: (8/2/01 11:48:12 AM)

* @author: Administrator

*/

public class Test {

/**

* Test constructor comment.

*/

public Test() {

super();

}

/**

* Insert the method's description here.

* Creation date: (8/2/01 11:50:26 AM)

* @param args java.lang.String[]

*/

public static void main(String[] args) {

try {

Test ts = new Test();

byte[] by = new byte[100];

java.io.DataInputStream bf =

new java.io.DataInputStream(ts.getClass().getResourceAsStream("/test.txt"));

System.out.println(bf.readLine());

} catch (Exception e) {

System.out.println(e.toString());

}

}

}

Pramod

pramod_bs at 2007-6-29 10:17:43 > top of Java-index,Desktop,Deploying...