Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jar data file problem

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I have created an executable Jar and want to include and read data from within the jar file for the application to use. Each time I run the application it does not find the data files. Any help to have a Jar application read a file within its own Jar would be appreciated.

I have tried the following and both did not work.

--------------------------------------------
try{
String soilline = null;
BufferedReader brsoils =
new BufferedReader( new FileReader(new File ("C:\\Program Files\\Calc\\Calc_DATA\\test.dat" )));
......
---------------------------------------------
try{
String soilline = null;
BufferedReader brsoils =
new FileReader(new File (System.getProperty("user.dir") + "\\test.dat" )));
.....
---------------------------------------------------
Thanks Ronnie
 
In the class needing to read the file...

Code:
InputStream is = this.getClass().getResourceAsStream("/yourfile");

This is assuming the file is at the top level in the Jar file.

Tim
 
Thanks...Would this also work if I am running the program via an IDE? Or would I need an if statement. Thanks again this was very ehlpful...Ronnie
 
As long as the jar is on the classpath of the application running, it will work. If running in an IDE, then you'd need to make the IDE aware of the jar, for example, as a library reference in JBuilder / Eclipse. If the jar contains the same classes that you are running under the IDE, then just check the generated classpath (easy in JBuilder - it is displayed in the console) to ensure that the directory containing the classes (which will be the most up-to-date, obviously) is earlier in the classpath than the jar reference.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top