在Eclipse上导出到runabble jar时,相对路径不起作用

时间:2018-06-06 14:38:31

标签: eclipse jar path runnable relative

我在Eclipse中做了一个Java项目。我正在使用一些相对路径,例如:

File htmlTemplateFile = new File("src/templateGeno2Pheno.html");

而不是绝对路径:

File htmlTemplateFile = new File("/Users/.../Documents/workspace/SeqAnalysis/src/templateGeno2Pheno.html");

当我使用Eclipse运行它时,一切都运行良好,但是一旦我将它导出到可运行的JAR并执行它,它就无法工作。

这是我的文件夹结构: enter image description here

这是我的代码: enter image description here

1 个答案:

答案 0 :(得分:0)

在打包到JAR之后,包括HTML模板在内的所有内容都被压缩到该JAR中,因此文件系统路径不适用于您。 Java没有像浏览器/ html那样的文件寻址系统。

一种选择是使用ClassLoader.getResourceAsStream API加载这些html模板。

将您的代码更改为:

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/templateGeno2Pheno.html");
String htmlString = IOUtils.toString(stream);

IOUtils来自Apache Commons IO库。

HTH

相关问题