Embedding resources (images, sound bits, etc) into a Java project then use those resources

后端 未结 1 342
说谎
说谎 2020-11-29 03:47

I have searched for a method of embedding a resource in a java project (using Eclipse v3.6.0) then using that embedded resource within a control (e.g., JLabel).

相关标签:
1条回答
  • 2020-11-29 03:57

    Just put those resources in the source/package structure and use ClassLoader#getResource() or getResourceAsStream() to obtain them as URL or InputStream from the classpath by the full qualified package path.

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("/image.gif");
    // ...
    

    Or if it is in the same package as the current class, you can also obtain it as follows:

    InputStream input = getClass().getResourceAsStream("image.gif");
    

    As a side question, how do I get Eclipse to create the project as a executable so it can be launched.

    Rightclick Java Project > Export > Runnable JAR File .

    0 讨论(0)
提交回复
热议问题