aspose file tools*
The moose likes Beginning Java and the fly likes read external text file from JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read external text file from JAR" Watch "read external text file from JAR" New topic
Author

read external text file from JAR

Andrew Lowcock
Greenhorn

Joined: Aug 13, 2003
Posts: 8
I am trying to access a properties file from within a jar file ...
InputStream is = getClass().getResourceAsStream("rehydration.properties");
and throw the following error (one of my own)-
Wed Aug 13 15:59:59 BST 2003 : Can't read the importer.properties file. Make sure rehydration.properties is in the right place.
Basically I can't find the resource file that I would like to be external to the jar file and in the same directory.
Is there a standard solution to this, or any good articles here I could refer to?
Many thanks in advance
Andrew
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
getResourceAsStream() (Actually all the ClassLoader getResource methods) actually searches the classpath. If you don't want rehydration.properties in the jar, to use the ClassLoader, you muyst put it in a directory specified in the classpath.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Andrew Lowcock
Greenhorn

Joined: Aug 13, 2003
Posts: 8
Thanks Carl,
But ... I am afraid that didn't work for me. I still can't find the resource file - I've placed it in the same directory as the JAR file and tried both
java -jar -classpath ./ XMLCandidateRehydration.jar
and
java -jar -classpath e:\testing\ XMLCandidateRehydration.jar
and neither finds the properties file. Is there an alternative to getResourceAsStream() to read in the properties file?
Thanks again.
Andrew
Originally posted by Carl Trusiak:
getResourceAsStream() (Actually all the ClassLoader getResource methods) actually searches the classpath. If you don't want rehydration.properties in the jar, to use the ClassLoader, you muyst put it in a directory specified in the classpath.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Indeed. If you use the "-jar" option, the -classpath option is ignored. Executable jar files are expected to be self-contained (except for things loaded by the boot class loader).
If you want to do this, then you'll have to use the class name of the main class in the jar file on the command line:
java -classpath XMLCandidateRehydration.jar;. com.me.MyClass


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read external text file from JAR