Including Images with an executable jar

前端 未结 2 1143
野性不改
野性不改 2020-11-27 22:17

I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet

I am packaging a quick game I made into a executable jar bu

相关标签:
2条回答
  • 2020-11-27 23:04

    Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.

    Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader

    background = ImageIO.read(getClass().getResource("/wood.jpeg"));
    

    Should work...

    This will return a URL which ImageIO can use to load the resource.

    You could also have a read of

    • Classpath resource within jar
    • Jar get image as resource
    • Load a resource in Jar

    And I could list some more. So, yeah, it gets asked a lot ;)

    0 讨论(0)
  • 2020-11-27 23:13

    Try using Constructing Runnable Jar using the Eclipse.

    R_Click on the Project in the Package Explorer ---> 
    Export ---> Runnable JAR file ---> 
    Select the option of Package required libraries into generated JAR
    
    0 讨论(0)
提交回复
热议问题