Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read external text file from JAR

 
Andrew Lowcock
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Lowcock
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic