简体   繁体   中英

Access resource from inside jar

I got a jar file test.jar that contains a folder resources which contains txtFile.txt.

I'm trying to access the file but the file seems to be null.

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();
    }
}

Try using getResource("resources/txtFile.txt"); (ie without the first / ).

There should not be a leading slash when using the ClassLoader 's version of getResource , it will be interpreted as an absolute path always.

If you are using maven, src/main/resources and src/test/resources are txtFile.txt is there in either of the two, you don't need resources/ in path. You could use this insstead

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

Instead of using stream as File, you can request for InputStream using following:

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

You don't have to specify the resource folder . You can directly user the file name as

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

So i did this

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.

Conclusion: It worked when I used getClass().getResource("/resources/txtFile.txt") or getClass().getClassLoader().getResource("resources/txtFile.txt") ( getResourceAsStream() also works).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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