Exported Jar file won't read file inside jar

后端 未结 2 1482
说谎
说谎 2021-01-27 17:59

In the code sample below, when I test the code in Eclipse it works just fine. However, when I export the jar file and test it via the command line, it throws an error: IIO

相关标签:
2条回答
  • 2021-01-27 18:27

    You should try to check if your class is in the same directory than Images inside your jar.

       |
       |- Your class
       |- Images
           |- questionMark.png
    

    Also, have you tried using directly your url object ?

    File file = new File(url);
    
    0 讨论(0)
  • 2021-01-27 18:29

    try this

    public void test() {
        try(InputStream is = getClass().getResourceAsStream("Images/questionMark.png")) {
            ImageIO.read(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
提交回复
热议问题