java - 在 jar 文件中查找文件时出现问题

标签 java apache ant jar

我是一名 Apache Ant 新手,我创建了一个 Java 应用程序,其中包含许多文件,其中包括 sys.properties 文件。该文件的位置位于根目录中,为了被调用,代码中使用字符串“sys.properties”来定位该文件。它运行完美。

现在我已经使用 Apache Ant 创建了一个构建文件,该文件从应用程序中创建了一个 jar 文件。在我使用的代码中

<filelist dir="${basedir}" files="sys.properties"/>

当创建此 jar 文件时,它确实将新文件放置在 jar 文件的根目录中。

当我通过命令行运行时:

java -classpath C:\tmp\APP;doddle.jar; doddle.home.start

jar 应用程序似乎在寻找 sys.properties,但找不到它(doddle.home.start 类调用 sys.properties)。

知道问题出在哪里吗?提前致谢!

最佳答案

问题是 sys.properties 不再是一个文件,而是一个 jar 条目。

您可以通过 InputStream 访问它通过 SomeClass.class.getResourceAsStream("/sys.properties") (有关如何处理流的信息,请参阅 the I/O tutorial)

关于java - 在 jar 文件中查找文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7232260/

相关文章:

python - 错误 2006 : "MySQL server has gone away" using Python, Bottle Microframework 和 Apache

ant - 如果满足两个条件,则执行 Ant 任务

java - 在 Ant 任务中运行 "headless Eclipse"

java - Apache Ant 有没有办法在构建后更新 jar 文件?

java - Webdriver 预期条件失败 : waiting for element to no longer be visiblr

Java - 使用 repaint() 时闪烁

java - 从 SignedObject 获取时出现 InvalidObjectException

apache - 无法使用 chkconfig 添加 Apache 以在启动时启动(opensuse 13.1)

linux - Bash 命令未在 Apache CGI Shell 中运行

java - 关闭和回调