从jar内部访问资源

时间:2015-06-21 09:13:59

标签: java jar classloader

我有一个jar文件test.jar,其中包含一个包含txtFile.txt的文件夹资源。

我正在尝试访问该文件,但该文件似乎为空。

package main;

import java.net.URL;

public class Test {

    private Test() {
        URL file = this.getClass().getClassLoader().getResource("/resources/txtFile.txt");
        System.out.println(file == null);
    }

    public static final void main(String[] args) {
        new Test();
    }
}

5 个答案:

答案 0 :(得分:1)

尝试使用getResource("resources/txtFile.txt");(即没有第一个/)。

使用ClassLoader的{​​{1}}版本时,不应该有一个前导斜杠,它将被解释为绝对路径。

答案 1 :(得分:0)

如果你正在使用maven,src/main/resourcessrc/test/resources是txtFile.txt在两者之一,你在路径中不需要resources/。你可以使用这个

this.getClass().getClassLoader().getResource("/txtFile.txt");

答案 2 :(得分:0)

您可以使用以下命令来请求InputStream,而不是将stream用作File:

InputStream in = getClass().getResourceAsStream("/txtFile.txt"); 
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

答案 3 :(得分:0)

您不必指定资源文件夹。您可以直接将文件名用作

getClass().getResourceAsStream("txtFile.txt"); 

答案 4 :(得分:0)

所以我做了这个

package main;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Test {

    private Test() {
        Class<?> c = this.getClass();
        ClassLoader cl = c.getClassLoader();
        InputStream[] iss = new InputStream[8];
        URL[] urls = new URL[8];
        iss[0] = c.getResourceAsStream("/resources/txtFile.txt");
        iss[1] = c.getResourceAsStream("resources/txtFile.txt");
        iss[2] = c.getResourceAsStream("/txtFile.txt");
        iss[3] = c.getResourceAsStream("txtFile.txt");
        iss[4] = cl.getResourceAsStream("/resources/txtFile.txt");
        iss[5] = cl.getResourceAsStream("resources/txtFile.txt");
        iss[6] = cl.getResourceAsStream("/txtFile.txt");
        iss[7] = cl.getResourceAsStream("txtFile.txt");
        urls[0] = c.getResource("/resources/txtFile.txt");
        urls[1] = c.getResource("resources/txtFile.txt");
        urls[2] = c.getResource("/txtFile.txt");
        urls[3] = c.getResource("txtFile.txt");
        urls[4] = cl.getResource("/resources/txtFile.txt");
        urls[5] = cl.getResource("resources/txtFile.txt");
        urls[6] = cl.getResource("/txtFile.txt");
        urls[7] = cl.getResource("txtFile.txt");
        for (int i = 0; i < 8; i++)
            System.out.println("iss[" + i + "] is "
                    + String.valueOf(iss[i] == null));
        for (int i = 0; i < 8; i++)
            System.out.println("url[" + i + "] is "
                    + String.valueOf(urls[i] == null));
        int read;
        try {
            while ((read = iss[0].read()) != -1)
                System.out.print((char) read);
        } catch (IOException e) {
        }

    }

    public static final void main(String[] args) {
        new Test();
    }
}

Outprint:

iss[0] is false
iss[1] is true
iss[2] is true
iss[3] is true
iss[4] is true
iss[5] is false
iss[6] is true
iss[7] is true
url[0] is false
url[1] is true
url[2] is true
url[3] is true
url[4] is true
url[5] is false
url[6] is true
url[7] is true
This is sample text.

结论:当我使用getClass().getResource("/resources/txtFile.txt")getClass().getClassLoader().getResource("resources/txtFile.txt")getResourceAsStream()也有效)时,它有效。