Search code examples
javaantpathapache-commons

Leading backslash in path names in java


So, I am trying to code a regular Java application which reads the current revision from a file which is updated by ANT during compile time. When run on my dev machine (Eclipse 3.5.2 on Ubuntu 11.04) with either the OpenJDK or SunJDK, it throws a FileNotFoundException. Adding or removing a leading backslash seems to have no effect.

Any ideas on how I could solve this? I believe the fault lies in this line here:

in = new FileInputStream("data/build_info.properties");

Code- Updated

Transitioned to Java Properties

String revision = "";

Properties defaultProps = new Properties();
FileInputStream in;
try {
    in = new FileInputStream("data/build_info.properties");
    defaultProps.load(in);
    in.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
    
revision = "Version: " + defaultProps.getProperty("build.major.number") + "." +
defaultProps.getProperty("build.minor.number") + "    " +
"Revision: " + defaultProps.getProperty("build.revision.number");

ANT Jar Script

<target name="jar">
    <antcall target="clean" />
    <antcall target="compile" />

    <jar destfile="${dir.dist}/${name.jar}" basedir="." includes="${dir.lib}/*" filesetmanifest="mergewithoutmain">
        <manifest>
            <attribute name="Main-Class" value="emp.main.EmpowerView" />
        </manifest>
        <fileset dir="${dir.build}" includes="**/*" excludes="META-INF/*.SF" />
        <fileset dir="." includes="${dir.media}/*" />
        <fileset dir="." includes="${dir.data}/*" />
    </jar>
    <chmod file="${dir.dist}/${name.jar}" perm="+x" />

</target>

Solution

  • If you're trying to load a file inside of the Jar, then you need to use java.lang.Class.getResourceAsStream() to load the file. You can't point to a file that's in the Jar with java.util.File. For an example on how to use it, see this answer:

    https://stackoverflow.com/questions/4548791