1

在我的 Eclipse JUNIT 测试中,我有几个文本输入文件由

new BufferedReader(new FileReader(inputFileName));.  

它们被放置在项目的根目录中 将这个项目导出到 jar 之后,这些文件被放置到 jar 根目录中。
我从 LINUX 运行 JUNIT 测试为

java -cp "<my jar>:junit-4.8.2.jar:jaxb-impl.jar" junit.textui.TestRunner <test class name>

但是,在这种情况下,我得到 java.io.FileNotFoundException 在 LINUX 上解决此问题的最佳方法是什么,同时仍然能够从 Eclipse 运行测试?

4

3 回答 3

2

使用 Class#getResourceAsStream() 方法加载类路径资源:

InputStream is = this.getClass().getResourceAsStream("myfile.txt");

您可以Reader使用InputStreamReader适配器将其转换为接口。

于 2013-05-14T15:29:44.200 回答
1

What I want was achieved by adding * to the classpath and putting properties file into run directory. This feature started in Java 6. Then the properties file is achievable in classpath and is achieved both from Eclipse and command prompt running. It is a little different from my question – the properties file is not in jar, but that is what is needed. Plus, one can edit the properties file outside of Eclipse

于 2013-05-16T19:03:31.330 回答
0

您可以使用类方法getResourceAsStream

stackoverflow 上还有另一个问题,请参见: getResourceAsStream() vs FileInputStream

于 2013-05-14T15:31:31.997 回答