将资源(图像、声音等)嵌入到 Java 项目中,然后使用这些资源
我搜索了一种在 java 项目中嵌入资源的方法(使用 Eclipse v3.6.0),然后在控件中使用该嵌入资源(例如,JLabel
)。我见过从文件系统引用资源的方法。项目开发完成后,我想将应用程序发布为可执行文件。应该注意的是,这些可执行文件将部署/启动到 Windows、*NIX 和 Linux 平台。
我知道这可以在 Visual Studio 世界中完成,但我非常不熟悉如何在 Java/Eclipse IDE 中完成此操作。作为一个附带问题,如何让 Eclipse 将项目创建为可执行文件以便可以启动它?
非常感谢任何帮助。
马克
更新1:
根据BalusC的回复,我想分享我必须解决我的问题的代码。我的类位于“Viking.Test
”包下,然后我将图像文件放在“Viking.Test.Resources
”包下。这一切都是在 Eclipse 中完成的,以便将图像导入到项目中。
- 我通过右键单击“Project ->”导入图像导入->导入源的常规/文件系统。
- 选择了包含要导入的图像的文件夹
- 为“进入文件夹”参数选择了“
Project/src/Viking/Test/Resources
” - 未更改任何选项并单击“完成”
在源文件中,我添加了以下代码以将图像插入到 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 ) { }
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.
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?
Any help is greatly appreciated.
Mark
UPDATE 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.
- I imported the image by right-clicking on the Project -> Import -> General/File System for 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"
In the source file I added the following code to insert the image into a 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 ) { }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将这些资源放入源/包结构中并使用
ClassLoader#getResource()
或getResourceAsStream()
以URL
形式获取它们或来自类路径的InputStream
通过完全限定的包路径。或者如果与当前类在同一个包中,也可以通过如下方式获取:
右键单击 Java 项目 >出口>可运行的 JAR 文件。
Just put those resources in the source/package structure and use
ClassLoader#getResource()
orgetResourceAsStream()
to obtain them asURL
orInputStream
from the classpath by the full qualified package path.Or if it is in the same package as the current class, you can also obtain it as follows:
Rightclick Java Project > Export > Runnable JAR File .