wood burning stoves 2.0*
The moose likes Ant, Maven and Other Build Tools and the fly likes java.io.FileNotFoundException: properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "java.io.FileNotFoundException: properties" Watch "java.io.FileNotFoundException: properties" New topic
Author

java.io.FileNotFoundException: properties

Kevin Mayer
Greenhorn

Joined: Jan 28, 2009
Posts: 7
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30369
    
150

This seems like more of an Ant question than a JUnit one. Moving to our build tools forum.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

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()));


JBoss In Action
Kevin Mayer
Greenhorn

Joined: Jan 28, 2009
Posts: 7
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
 
subject: java.io.FileNotFoundException: properties