如何将图像捆绑在 jar 文件中 [英] How to bundle images in jar file

查看:23
本文介绍了如何将图像捆绑在 jar 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 java 应用程序.并将所有类捆绑在 jar 文件中..wen 我从 netbeans 运行该项目我的应用程序运行成功..但是我将我的 .jar 文件放在另一个地方并从那里运行..i没有得到我的应用程序使用的图标..在代码中,我从项目文件夹中的图像目录中获取我的图标.

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.

现在,我想知道我们如何将这些图像文件呈现给最终用户(就像我们呈现 .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

推荐答案

这里好像有两个问题:

  1. 如何让 NetBeans 在构建项目时生成的 jar 中包含图像文件?

  1. How do I get NetBeans to include an image file in the jar produced when I build my project?

如何从 jar 访问图像文件?

How do I access an image file from a jar?

此答案适用于 NetBeans 6.8 并解决了两个子问题.

This answer applies to NetBeans 6.8 and addresses both of the subquestions.

假设您有一个基于 ant 的 Java 应用程序项目.

Assume that you have an ant based Java Application Project.

这是项目的文件"视图

JP
+ images
  + test.jpg
+ nbproject
+ src
  + jp
    + Main.java
+ test
+ build.xml
+ manifest.mf

在你的 Main.java 中,你有这样的代码:

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());

当您从 NB 内部运行此项目时,您会得到以下输出:

When you run this project from inside NB you get this output:

/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true

当您运行打包到 jar 中的代码时,您会得到如下内容:

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

为了在执行 jar 时获得更好的效果,您需要执行以下操作:

To get something better when the jar is executed, you need to do the following:

将图像目录添加为项目的源根目录.

右键单击项目并选择属性项.将出现一个对话框.

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.

按下出现在源包文件夹"表旁边的添加文件夹..."按钮.将出现一个 FileChooser.

Press the 'Add Folder...' button that appears next to the 'Source Package Folders' table. A FileChooser will appear.

使用此选择器选择图像文件夹,然后按 OK 按钮.图像文件夹的条目将被添加到表格中.

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.

更改您的代码以使用 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);

当您从 IDE 内部运行项目时,您应该看到如下内容:

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

当你运行打包到 jar 中的代码时,你会得到这样的东西:

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

获取test.jpg文件的URL后,可以使用ImageIcon(URL) 创建图标

After you get the URL for the test.jpg file, you can use ImageIcon(URL) to create the icon

这篇关于如何将图像捆绑在 jar 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆