getClass().getResource(“/”) returns null in command line

后端 未结 2 764
死守一世寂寞
死守一世寂寞 2020-12-19 16:44

I\'m trying to read a file in my maven project at /src/main/resources/file.txt.

I\'m using

URL url=this.getClass().getResource(\"/\");
String filePa         


        
相关标签:
2条回答
  • 2020-12-19 16:56

    There are (often) no directories inside jar files. Therefor it will return null.


    If you want to get the file you could get that resource directly:

    URL fileUrl = getClass().getResource("/file.txt");
    ...
    

    Or simply:

    InputStream fileInputStream = getClass().getResourceAsStream("/file.txt");
    
    0 讨论(0)
  • 2020-12-19 17:16

    You should move that file into your CLASSPATH and get it like this:

    InputStream is = this.getClass().getResourceAsStream("file.txt");
    
    0 讨论(0)
提交回复
热议问题