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");
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");
<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>
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.
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.