如何在jar文件中捆绑图像 [英] How to bundle images in jar file
问题描述
我制作了一个java应用程序。并捆绑了jar文件中的所有类。我从netbeans运行项目我的应用程序运行成功..但是我把我的.jar文件放在另一个地方并从那里运行.i我没有得到我的应用程序使用的图标..在代码中,我从项目文件夹中的图像目录中获取我的图标。
现在,我想知道我们怎么能将这些图像文件呈现给最终用户(就像我们提供的.jar文件一样)。谢谢提前
好像有这里有两个问题:
-
如何让NetBeans在我构建项目时生成的jar中包含图像文件? / p>
-
如何从jar访问图像文件?
这个答案适用于NetBeans 6.8并解决了这两个子问题。
假设你有一个基于ant的Java应用程序项目。
这是项目的文件视图
JP
+ images
+ test.jpg
+ nbproject
+ src
+ jp
+ Main.java
+ test
+ build.xml
+ manifest.mf
在你的Main.java中你有这样的代码:
public static void main(String [] args)throws IOException {
//在文件系统中查找文件..可能不是一个好主意
文件f =新文件(images / test.jpg) ;
System.out.println(f.getCanonicalPath()++ f.exists());
当您从NB内部运行此项目时,您将获得此输出:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
当您运行包装到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
要在执行jar时获得更好的效果,您需要执行以下操作:
将images目录添加为项目的源根。
右键单击项目并选择Properties项。将出现一个对话框。
在对话框左侧的列表中选择Sources。这将更改对话框右侧面板的内容。
按下源包文件夹旁边的添加文件夹...按钮'表。将出现FileChooser。
使用此选择器选择images文件夹,然后按OK按钮。将添加images文件夹的条目表。
使用项目属性对话框中的确定按钮接受更改并关闭对话框。
更改代码以使用 Class.getResource() 。
public static void main(String [] args)throws IOException {
//在文件系统中查找文件..可能不是个好主意
文件f =新文件(images / test.jpg放在);
System.out.println(f.getCanonicalPath()++ f.exists());
URL url = Main.class.getResource(/ test.jpg);
System.out.println(url);
当您从IDE内部运行项目时,您应该看到如下内容:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
file:/ export / home / vkraemer / NetBeansProjects / JavaApplication2 / images / test.jpg
当您运行包装到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
获得test.jpg文件的URL后,可以使用 ImageIcon(URL)创建图标
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.
Now,i wanted to know how can we present these image files to the end user (like we present .jar file).Thanks in advance
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?
This answer applies to NetBeans 6.8 and addresses both of the subquestions.
Assume that you have an ant based Java Application Project.
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:
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:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
When you run the code packed into the jar, you get something like this:
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:
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.
Use this chooser to select the images folder and press the OK button. An entry for the images folder will be added table.
Use the OK button on the Project Properties dialog to accept the changes and dismiss the dialog.
Change your code to use 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:
/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:
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
这篇关于如何在jar文件中捆绑图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!