创建一个File对象,表示相对于.jar文件根目录的目录

时间:2013-01-22 21:53:15

标签: java file executable-jar robolectric

我正在尝试运行在可执行.jar文件中启动的Robolectric测试。 Robolectric requires您将文件传递给包含AndroidManifest.xml文件的目录的构造函数。我的AndroidManifest.xml文件位于我的.jar文件的根目录下,测试运行器位于com.some.package之类的包中。我怎样才能做到这一点?

我尝试了以下内容:

public class CustomTestRunner extends RobolectricTestRunner {
    public CustomTestRunner(Class testClass) throws InitializationError, URISyntaxException {
        super(testClass, new File(CustomTestRunner.class.getResource("/AndroidManifest.xml").getPath().replace("AndroidManifest.xml", "")));
    }
}

哪个不起作用,当它运行Robolectric抱怨时,我得到这个FileNotFoundException:

  

java.io.FileNotFoundException:   /Users/christopherperry/Desktop/file:/Users/christopherperry/Desktop/my-executable-jar.jar!/AndroidManifest.xml   找不到或没有文件;它应该指向你的项目   AndroidManifest.xml在   com.xtremelabs.robolectric.RobolectricConfig.validate(RobolectricConfig.java:73)     在   com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:602)

我不明白,因为AndroidManifest.xml文件位于jar文件的根目录中,并且从错误消息中看起来就是它所在的位置。

1 个答案:

答案 0 :(得分:0)

JAR文件中的项目本身不是文件,因此答案是“你不能”。

您可以通过从Class.getResource()获取的URL或从Class.getResourceAsStream()获取的流来访问它们。