Java 如何将图像捆绑在 jar 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2273040/
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
How to bundle images in jar file
提问by krishna
I made a java application.and bundled all classes in jar file..wen i run the project from netbeans my app is running successfully..but wen i place my .jar file at another place and run from there..i am not getting the icons used by my application..In the code i get my icons from images directory present in project folder.
我制作了一个 java 应用程序。并将所有类捆绑在 jar 文件中..我从 netbeans 运行该项目我的应用程序运行成功..但是我将我的 .jar 文件放在另一个地方并从那里运行..我没有得到我的应用程序使用的图标..在代码中,我从项目文件夹中的图像目录中获取我的图标。
Now,i wanted to know how can we present these image files to the end user (like we present .jar file).Thanks in advance
现在,我想知道我们如何将这些图像文件呈现给最终用户(就像我们呈现 .jar 文件一样)。提前致谢
采纳答案by vkraemer
It seems like there are two questions here:
这里似乎有两个问题:
How do I get NetBeans to include an image file in the jar produced when I build my project?
How do I access an image file from a jar?
如何让 NetBeans 在构建项目时生成的 jar 中包含图像文件?
如何从 jar 访问图像文件?
This answer applies to NetBeans 6.8 and addresses both of the subquestions.
此答案适用于 NetBeans 6.8 并解决了两个子问题。
Assume that you have an ant based Java Application Project.
假设您有一个基于 ant 的 Java 应用程序项目。
Here is the 'Files' view of the project
这是项目的“文件”视图
JP
+ images
+ test.jpg
+ nbproject
+ src
+ jp
+ Main.java
+ test
+ build.xml
+ manifest.mf
Inside your Main.java you have code like this:
在 Main.java 中,您有这样的代码:
public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
When you run this project from inside NB you get this output:
当您从 NB 内部运行此项目时,您会得到以下输出:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
When you run the code packed into the jar, you get something like this:
当您运行打包到 jar 中的代码时,您会得到如下内容:
bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.txt false
To get something better when the jar is executed, you need to do the following:
为了在执行 jar 时获得更好的效果,您需要执行以下操作:
Add the images directory as a source root for you project.
将图像目录添加为您的项目的源根目录。
Right click on the project and select the Properties item. A dialog will appear.
右键单击项目并选择属性项。将出现一个对话框。
Select 'Sources' in the list that is on the left side of the dialog. This will change the content of the panel on the right side of the dialog.
在对话框左侧的列表中选择“来源”。这将更改对话框右侧面板的内容。
Press the 'Add Folder...' button that appears next to the 'Source Package Folders' table. A FileChooser will appear.
按出现在“源包文件夹”表旁边的“添加文件夹...”按钮。将出现一个 FileChooser。
Use this chooser to select the images folder and press the OK button. An entry for the images folder will be added table.
使用此选择器选择图像文件夹,然后按 OK 按钮。图像文件夹的条目将添加到表中。
Use the OK button on the Project Properties dialog to accept the changes and dismiss the dialog.
使用“项目属性”对话框上的“确定”按钮接受更改并关闭对话框。
Change your code to use Class.getResource().
更改您的代码以使用Class.getResource()。
public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
URL url = Main.class.getResource("/test.jpg");
System.out.println(url);
When you run the project from inside the IDE, you should see something like this:
当您从 IDE 内部运行项目时,您应该看到如下内容:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg
When you run the code packed into the jar, you will get something like this:
当您运行打包到 jar 中的代码时,您将得到如下内容:
bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.jpg false
jar:file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar!/test.jpg
After you get the URL for the test.jpg file, you can use ImageIcon(URL)to create the icon
获得 test.jpg 文件的 URL 后,可以使用ImageIcon(URL)创建图标
回答by mh000ht
For a Maven Project in NetBeans 6.9, put your filen into Resources in the "Other Sources" folder of the Project.
对于 NetBeans 6.9 中的 Maven 项目,将您的文件放入项目的“Other Sources”文件夹中的 Resources 中。
And be sure you dont forget the leading "/"
并确保您不要忘记前导“/”