Java Images not drawn when running java -jar

后端 未结 2 412
暖寄归人
暖寄归人 2020-12-12 07:22

I have a problem i have never had before and couldnt find a solution on the web. I have a small Programm which uses some images to print a menu.

This is the Class i

相关标签:
2条回答
  • 2020-12-12 07:54

    The ".getPath()" part of your code excludes the leading part of the URL.
    This needs to be present if your resource is part of a jar file.

    I suggest you remove the ".getPath()" and use the full URL.
    Printing out the full URL is a good idea too, in a System.out.println for example.

    0 讨论(0)
  • 2020-12-12 07:56

    Before all be sure your resource is correctly loaded (for example with a System.out())!

    Instead to use ImageIcon(String location) use ImageIcon(URL location) constructor because your image is not on hdd, but live compressed as URL in your classpath (something like MyJar.jar!/path/to/image.png"); you have to modify your image loading as

    this.getClass().getClassLoader().getResource("MyImage.png");
    
    0 讨论(0)
提交回复
热议问题