开发者

Leading backslash in path names in java

开发者 https://www.devze.com 2023-03-22 20:44 出处:网络
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)

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&开发者_开发问答quot; />
        <fileset dir="." includes="${dir.media}/*" />
        <fileset dir="." includes="${dir.data}/*" />
    </jar>
    <chmod file="${dir.dist}/${name.jar}" perm="+x" />

</target>


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


Can you step through with the debugger? I notice you have some System.out.println's - are they being printed when uncommented?

Regardless, I don't see how revision is null -- it could be an empty String due to an IOException, but not null.


What happening here is as follows, the call to newfile() or FileUtils.readLines() is throwing an IOException.
This is being caught at :

} catch (IOException e) {
    revision = "" + e.getCause();
}

and is setting revision = the value of e.getCause() prefixed by an empty String.

e.getCause() is returning the text null, which leads me to believe that the exception is being thrown by FileUtils.readLines().
Test this by changing revision = "" + e.getCause(); to revision = "XXX->" + e.getCause();, after which I expect you'll find that the value of revision after the call is = "XXX->null".

The IOException with cause=null is being thrown because the file data/build_info.properties cannot be located.

So you need to check the value of your current working directory when you run this code, and ensure that data/build_info.properties can be resolved relative to that path.

0

精彩评论

暂无评论...
验证码 换一张
取 消