[英]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. 因此,我正在尝试编写一个常规Java应用程序,该应用程序从编译期间由ANT更新的文件中读取当前版本。 When run on my dev machine (Eclipse 3.5.2 on Ubuntu 11.04) with either the OpenJDK or SunJDK, it throws a FileNotFoundException
. 当使用OpenJDK或SunJDK在我的开发机器(Ubuntu 11.04上的Eclipse 3.5.2)上运行时,它会抛出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? 我注意到你有一些System.out.println - 是否在取消注释时打印?
Regardless, I don't see how revision is null -- it could be an empty String due to an IOException, but not null. 无论如何,我没有看到修订版如何为null - 由于IOException,它可能是一个空字符串,但不是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. 如果您尝试在Jar内部加载文件,则需要使用java.lang.Class.getResourceAsStream()
来加载文件。 You can't point to a file that's in the Jar with java.util.File
. 您不能使用java.util.File
指向Jar java.util.File
。 For an example on how to use it, see this answer: 有关如何使用它的示例,请参阅以下答案:
https://stackoverflow.com/questions/4548791 https://stackoverflow.com/questions/4548791
What happening here is as follows, the call to newfile()
or FileUtils.readLines()
is throwing an IOException. 这里发生的事情如下,对newfile()
或FileUtils.readLines()
的调用抛出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. 并设置revision =以空字符串为前缀的e.getCause()
的值。
e.getCause()
is returning the text null , which leads me to believe that the exception is being thrown by FileUtils.readLines()
. e.getCause()
的返回文本空 ,这使我相信,异常正被抛出FileUtils.readLines()
Test this by changing revision = "" + e.getCause();
通过更改revision = "" + e.getCause();
to revision = "XXX->" + 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". ,之后我希望你会发现调用后修订的值是=“XXX-> null”。
The IOException with cause=null is being thrown because the file data/build_info.properties
cannot be located. 抛出带有cause = null的IOException,因为无法找到文件data/build_info.properties
。
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. 因此,在运行此代码时需要检查当前工作目录的值,并确保可以相对于该路径解析data/build_info.properties
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.