【问题标题】:Create a File object representing a directory relative to the root of a .jar file创建一个 File 对象,表示相对于 .jar 文件的根目录的目录
【发布时间】:2013-01-22 21:53:14
【问题描述】:

我正在尝试运行在可执行 .jar 文件中启动的 Robolectric 测试。 Robolectric requires 您将 File 传递给其构造函数,其目录包含 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 文件的根目录中,从错误消息来看,这似乎是它正在寻找的位置。

【问题讨论】:

    标签: java file executable-jar robolectric


    【解决方案1】:

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

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

    【讨论】:

    • @ChristopherPerry JAR 文件不是文件系统。如果这是您的意图,您不能在不完全重建的情况下给他们写信。重新考虑您的要求。
    • 我不想写到 jar 中,请重新阅读我的问题。 Robolectric 正在尝试读取打包在 jar 中的文件。这是 Robolectric 的要求,不是我的。
    猜你喜欢
    • 1970-01-01
    • 2022-08-04
    • 2021-05-31
    • 2016-11-24
    • 2019-07-21
    • 2020-11-01
    • 2012-11-13
    • 2018-06-16
    • 2017-03-03
    相关资源
    最近更新 更多