将资源(图像、声音位等)嵌入到 Java 项目中,然后使用这些资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3721706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embedding resources (images, sound bits, etc) into a Java project then use those resources
提问by lordhog
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
). I have seen methods of referencing resources from the file system. Once the project has been developed I would like to publish the application as an executable. It should be noted these executables will be deployed/launched to Windows, *NIX, and Linux platforms.
我搜索了一种在 java 项目中嵌入资源的方法(使用 Eclipse v3.6.0),然后在控件中使用该嵌入资源(例如,JLabel
)。我见过从文件系统中引用资源的方法。项目开发完成后,我想将应用程序发布为可执行文件。应该注意的是,这些可执行文件将部署/启动到 Windows、*NIX 和 Linux 平台。
I know this can be done in the Visual Studio world, but I'm very unfamiliar how to do this in Java/Eclipse IDE. As a side question, how do I get Eclipse to create the project as a executable so it can be launched?
我知道这可以在 Visual Studio 世界中完成,但我非常不熟悉如何在 Java/Eclipse IDE 中执行此操作。作为一个附带问题,我如何让 Eclipse 将项目创建为可执行文件以便它可以启动?
Any help is greatly appreciated.
任何帮助是极大的赞赏。
Mark
标记
UPDATE 1:
更新1:
Based upon BalusC's response, I wanted to share the code I have to resolve my problem. My classes are under the package of "Viking.Test
" and then I placed the image file under the package "Viking.Test.Resources
". This is all done within Eclipse to import the image into the project.
根据BalusC 的回应,我想分享我必须解决的问题的代码。我的课程在“ Viking.Test
”包下,然后我将图像文件放在“ Viking.Test.Resources
”包下。这一切都在 Eclipse 中完成,以将图像导入到项目中。
- I imported the image by right-clicking on the Project -> Import -> General/File Systemfor the import source.
- Selected the folder which contained the image to import
- Selected "
Project/src/Viking/Test/Resources
" for the 'Into folder'parameter - Didn't change any of the options and clicked "Finished"
- 我通过右键单击导入源的项目 -> 导入 -> 常规/文件系统来导入图像。
- 选择包含要导入的图像的文件夹
Project/src/Viking/Test/Resources
为“进入文件夹”参数选择“ ”- 没有更改任何选项并单击“完成”
In the source file I added the following code to insert the image into a JLabel
(LblLogo
)
在源文件中,我添加了以下代码以将图像插入JLabel
( LblLogo
)
try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream(
"Viking/Test/Resources/MyImage.jpg");
Image logo = ImageIO.read(input);
LblLogo = new JLabel( new ImageIcon( logo ) );
LblLogo.setBounds(20, 11, 210, 93);
getContentPane().add(LblLogo);
}
catch ( IOException e ) { }
采纳答案by BalusC
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#getResource()
或getResourceAsStream()
获得他们作为URL
或InputStream
由完全限定包路径的类路径。
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.
作为一个附带问题,我如何让 Eclipse 将项目创建为可执行文件,以便可以启动它。
Rightclick Java Project > Export > Runnable JAR File.
右键单击 Java 项目 > 导出 > 可运行 JAR 文件。