GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Reading external files from a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading external files from a jar" Watch "Reading external files from a jar" New topic
Author

Reading external files from a jar

Kevin Waddell
Greenhorn

Joined: Mar 07, 2008
Posts: 3
Greetings,

It seems that under the Linux GUI (Gnome) a Java .jar cannot read external files that are in the same directory (relative path) as the .jar. This is because when running anything in gnome, the current directory defaults to $HOME so tries to find the files there. Under Windows and Mac, this does not happen and the .jar can read external files perfectly. I can also run java -jar ProgName.jar from the linux command line and it runs perfectly. How do I get around this problem with running in the Linux GUI? Here are some of my stipulations.

1. I do not want to put my external files in a jar
2. I want to access these files with a path relative to the jar
3. I want to make it easy for the user to download and run

Should I just not worry about a jar file, and just distribute the class files instead? You may have a look at http://adornthetruth.com/nikamo to get a better idea of the program I am working on. I currently distribute it as a zip file that has to be unzipped. In the zip file is the jar plus a folder containing the external files I want the jar to read in.

-Kevin
Aaron Johnson
Greenhorn

Joined: Mar 08, 2008
Posts: 3
Maybe you could use System.getProperty() with the appropriate property to find the directory the jar file is actually in. See http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getProperties()
Maybe user.dir or java.class.path or java.library.path

Let me know how it goes..

-Aaron
http://www.weknowerrors.com
A free Wiki for error codes and computer problems.
Kevin Waddell
Greenhorn

Joined: Mar 07, 2008
Posts: 3
Thanks Aaron!

The java.class.path seems to work. I had tried the user.dir before and it just gives me the same as user.home. Now my question is, currently java.class.path only has path. If at some point the class path had more than one path, would the path to my main jar always be the first one?

Thanks again, I've been pulling my hair out about this one for a while now.

-Kevin
 
GeeCON Prague 2014
 
subject: Reading external files from a jar