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

你说的曾经没有我的故事 提交于 2019-11-27 07:06:05

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 .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!