简体   繁体   English

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

[英]Create a File object representing a directory relative to the root of a .jar file

I'm trying to run Robolectric tests that are kicked off inside of an executable .jar file. 我正在尝试运行在可执行.jar文件内部启动的Robolectric测试。 Robolectric requires you to pass a File to its constructor with the directory which contains the AndroidManifest.xml file. Robolectric要求您将文件传递到带有包含AndroidManifest.xml文件的目录的构造函数。 My AndroidManifest.xml file sits at the root of my .jar file, and the test runner lives in a package something like com.some.package. 我的AndroidManifest.xml文件位于我的.jar文件的根目录下,测试运行器位于com.some.package之类的包中。 How can I achieve this? 我该如何实现?

I tried the following: 我尝试了以下方法:

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", "")));
    }
}

Which doesn't work, when it runs Robolectric complains, and I get this FileNotFoundException: 哪个不起作用,当它运行Robolectric时会抱怨,并且我得到这个FileNotFoundException:

java.io.FileNotFoundException: /Users/christopherperry/Desktop/file:/Users/christopherperry/Desktop/my-executable-jar.jar!/AndroidManifest.xml not found or not a file; java.io.FileNotFoundException:/Users/christopherperry/Desktop/file:/Users/christopherperry/Desktop/my-executable-jar.jar!/AndroidManifest.xml找不到或不是文件; it should point to your project's AndroidManifest.xml at com.xtremelabs.robolectric.RobolectricConfig.validate(RobolectricConfig.java:73) at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:602) 它应指向com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:602)上com.xtremelabs.robolectric.RobolectricConfig.validate(RobolectricConfig.java:73)上项目的AndroidManifest.xml。

I don't understand because the AndroidManifest.xml file IS in the root of the jar file, and from the error message this appears to be where it is looking. 我不明白,因为AndroidManifest.xml文件位于jar文件的根目录中,并且从错误消息中可以看到该错误所在。

Items in JAR files are not themselves files, so the answer is "you can't". JAR文件中的项目本身不是文件,因此答案是“您不能”。

You can access them via URLs obtained from Class.getResource(), or streams obtained from Class.getResourceAsStream(). 您可以通过从Class.getResource()获得的URL或从Class.getResourceAsStream()获得的流来访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM