我正在从我的Java项目的编译JAR中的包中加载一个文本文件。相关目录结构如下:

/src/initialization/Lifepaths.txt

我的代码通过调用Class::getResourceAsStream来返回一个InputStream来加载一个文件。

public class Lifepaths {
    public static void execute() {
        System.out.println(Lifepaths.class.getClass().
            getResourceAsStream("/initialization/Lifepaths.txt"));
    }

    private Lifepaths() {}

    //This is temporary; will eventually be called from outside
    public static void main(String[] args) {execute();}
}

不管我用什么,输出总是输出null。我不知道为什么上面的方法不管用,所以我也尝试了一下:

“初始化/ src / / Lifepaths.txt” “初始化/ Lifepaths.txt” “Lifepaths.txt”

这两种方法都不起作用。到目前为止,我已经阅读了许多关于这个主题的问题,但没有一个是有帮助的——通常,他们只是说使用根路径加载文件,而我已经这样做了。或者只是从当前目录加载文件(只是加载文件名),我也尝试过。该文件将被编译到JAR中的适当位置,并具有适当的名称。

我怎么解决这个问题?


当前回答

聚会迟到了。这取决于你使用的JDK版本(也许是时候查看StackOverflow“已经回答的问题”了?)JPMS会影响您能够加载(或不能加载)的资源。

将我的代码打包为模块,我必须这样做:

.getResourceAsStream .getModule .getClass Thread.currentThread()()()(“resourceName”);

然后就成功了

其他回答

我发现自己也遇到了类似的问题。因为我使用maven,我需要更新我的pom.xml,包括这样的东西:

   ...
</dependencies>
<build>
    <resources>
        <resource>
            <directory>/src/main/resources</directory>
        </resource>
        <resource>
            <directory>../src/main/resources</directory>
        </resource>
    </resources>
    <pluginManagement>
        ...

请注意其中的资源标记,以指定文件夹的位置。如果你有嵌套的项目(像我一样),那么你可能想从其他领域获得资源,而不仅仅是在你工作的模块中。如果您使用类似的配置数据,这有助于减少在每次回购中保持相同的文件

不要使用绝对路径,让它们相对于项目中的“资源”目录。快速和肮脏的代码,显示MyTest.txt的内容从目录“资源”。

@Test
public void testDefaultResource() {
    // can we see default resources
    BufferedInputStream result = (BufferedInputStream) 
         Config.class.getClassLoader().getResourceAsStream("MyTest.txt");
    byte [] b = new byte[256];
    int val = 0;
    String txt = null;
    do {
        try {
            val = result.read(b);
            if (val > 0) {
                txt += new String(b, 0, val);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    } while (val > -1);
    System.out.println(txt);
}

对我有用的是我把文件放在下面

src/main/java/myfile.log

and

InputStream is = getClass().getClassLoader().getResourceAsStream("myfile.log");
        
        if (is == null) {
            throw new FileNotFoundException("Log file not provided");
        }

我的问题是资源不在src文件夹。

因此,从jar中获取资源有几种方法,每种方法的语法略有不同,需要指定不同的路径。

我所看到的最好的解释是来自InfoWorld的这篇文章。我在这里总结一下,但如果你想了解更多,你应该看看这篇文章。

方法

ClassLoader.getResourceAsStream()。

格式:“/”-以名称分隔;没有前导“/”(所有名称都是绝对的)。

例如:this.getClass () .getClassLoader () .getResourceAsStream(“一些/ pkg / resource.properties”);

Class.getResourceAsStream ()

格式:“/”-以名称分隔;前导“/”表示绝对名称;所有其他名称都是相对于类的包

例如:this.getClass () .getResourceAsStream(" /一些/ pkg / resource.properties”);

2020年9月更新:更改文章链接。原文来自Javaworld,它现在托管在InfoWorld上(并且有更多广告)