简体   繁体   中英

InputStream from getResourceAsStream results in NULL

        InputStream input = Faculty.class.getClassLoader().getResourceAsStream("Resources\\Names.txt");
        DataInputStream in = new DataInputStream(input);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null && !strLine.equals("")) {
            keywords.add(strLine);
        }

The code above works in NetBeans. But when I packed the code into an executable Jar and ran it from command line, it gave me a NullPointerException because of the non-initialized input . So I suspect the Jar was not able to read the resource file packaged in it. The folder Resources is directly under source folder of my project. Can anyone give some hint what to try?

you should write like this:

getResourceAsStream("/Resources/Names.txt");

if you don't add the prefix '/', it means that your path is based on your class path, not the root path

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