Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.io.FileNotFoundException: properties

 
Kevin Mayer
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.We have a java application that is functioning correctly.

It uses the following code to load properties from a file.

private final static String PROPS_FILE = "properties/its/LoadDB.props";

readDbPropertiesFile(PROPS_FILE);

public void readDbPropertiesFile(String logPropsFile)
throws FileNotFoundException, IOException {
java.util.Properties props = new Properties();
props.load(new FileInputStream(logPropsFile));
}


2. When using the junit task in ant, it throws a FileNotFoundException saying.
java.io.FileNotFoundException: properties/its/LoadDB.props (No such file or directory)

3. The junit classpath in the build.xml is set as follows:
<path id="junit.classpath">
<pathelement location="${build.dir}/release/"/>
</path>

4. ${build.dir}/release/" is where the compiled code and properties files are located
ex.
build/release/properties/its/LoadDB.props

Can a second pair of eyes locate why the LoadDB.props cannot be found when running junit?

thank you

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like more of an Ant question than a JUnit one. Moving to our build tools forum.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, welcome to Java Ranch!

It looks to me like you are not loading the properties file via the classpath, but rather by using a relative file path. Thus the location you gave is used relative to the current working directory.

To load it via the classpath, use something like:

URL url = this.getClass().getClassLoader().findResource(PROPS_FILE);
props.load(new FileInputStream(url.toString()));
 
Kevin Mayer
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter for the insight as to using this.getClass().getClassLoader().findResource(PROPS_FILE). I found one other tidbit that might help someone if they are using the relative path. In the junit ant task, they can specify the directory where they want the jvm to execute from. You do this specifying fork='yes' and dir=. The dir is where the jvm will execute from.

ex.
<junit printsummary='yes' fork='yes' dir='${build.dir}/release' haltonfailure='yes' haltonerror='yes'>

For more information see http://ant.apache.org/manual/OptionalTasks/junit.html

It's nice to be welcomed here at the Java Ranch.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic