Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Accessing text files within a jar file (http://www.velocityreviews.com/forums/t369931-accessing-text-files-within-a-jar-file.html)

Blake Essing 09-11-2006 08:16 PM

Accessing text files within a jar file
 

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?= 09-11-2006 08:20 PM

Re: Accessing text files within a jar file
 
Blake Essing wrote:
> 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


All times are GMT. The time now is 01:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.