Accessing text files within a jar file

B

Blake Essing

I have a simple program that uses a text file for some static data.
When I run my program from within Eclipse or from the jar located in my
workspace it works fine because it can find the path to my development text
file. But I want to be able to run the jar from anywhere. How can I
access the file from within the jar file. Important bits of code I have
are:

private static File mFile = null;
private static String mBaseSolution = "../solution/Solutions.txt";

...

mFile = new File(mBaseSolutions);
if (mFile.exists()) {
...
} else {
System.out.println("Can't find solution file");
System.exit(1);
}

The code fails on the mFile.exists() because it can't find the correct
path.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Blake said:
I have a simple program that uses a text file for some static data.
When I run my program from within Eclipse or from the jar located in my
workspace it works fine because it can find the path to my development text
file. But I want to be able to run the jar from anywhere. How can I
access the file from within the jar file.

You can get an InputStream to a file inside a jar file
in the classpath via:

getClass().getClassLoader().getRessourceAsStream()

or one of the other similar variants.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top