使用Eclipse从类和图像创建可执行的JAR [英] Create executable JAR from classes and images using Eclipse

查看:165
本文介绍了使用Eclipse从类和图像创建可执行的JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件夹:

 游戏生活
| _bin
| | _Board.class
| | _Frame.class(main class)
| ...
| _res
| | _playToolbar.png
| | _pauseToolbar.png
| ...
| _src
| _Board.java
| _Frame.java
...

如何创建包含每个类和图像的可执行文件 .jar ,以便在执行 .jar 它运行框架类?我正在使用Eclipse。

解决方案

我认为最好把你的照片放在一个包里。这样,所有的东西都将从您的JAR文件中打包和加载。



之后,您将必须使用getResource(...)方法从类别



你将会有这样的一个:

  GameOfLife 
| - src
| | - 我的
| | | - 公司
| | | | - app
| | | | | Board.java
| | | | | Frame.java
| | | | | - 资源
| | | | | | - playToolbar.png
| | | | | | - pauseToolbar.png

然后从Frame类创建一个ImageIconplayToolbar.png将必须使用:

 新的ImageIcon(Frame.class.getResource(resources / playToolbar.png)); 

或:

 code> new ImageIcon(Frame.class.getResource(/ my / company / app / resources / playToolbar.png)); 

如果您使用Netbeans GUI构建器,它可以从包中加载资源而没有任何问题。 >

要自动运行Frame类,您必须创建一个MANIFEST.MF文件,并将其放在名为META-INF的文件夹中,位于JAR的根目录下。内部可以将框架定义为主类:

 清单版本:1.0 
主类:我的.company.app.Frame

如果您选择export-> Runnable Jar,Eclipse可以自动执行此步骤。


I have the following folder:

Game Of Life
|_bin
| |_Board.class
| |_Frame.class (main class)
|   ...
|_res
| |_playToolbar.png
| |_pauseToolbar.png
|   ...
|_src
  |_Board.java
  |_Frame.java
    ...

How do I create an executable .jar containing every class and image, so that when I execute the .jar it runs the Frame class? I am using Eclipse.

解决方案

I think it is best to put your pictures inside a package. This way, everything will be packaged and loaded from your JAR file.

Afterward you will have to load your images using the getResource(...) method from an instance of Class.

You will have something like this:

GameOfLife
|- src
|  |- my
|  |  |- company
|  |  |  |- app
|  |  |  |  | Board.java
|  |  |  |  | Frame.java
|  |  |  |  |- resources
|  |  |  |  |  |- playToolbar.png
|  |  |  |  |  |- pauseToolbar.png

Then to create an ImageIcon of "playToolbar.png" from the Frame class you will have to use:

new ImageIcon(Frame.class.getResource("resources/playToolbar.png"));

Or:

new ImageIcon(Frame.class.getResource("/my/company/app/resources/playToolbar.png"));

If you are using Netbeans GUI builder, it can load resources from package without any problem.

To autorun your Frame class you have to create a MANIFEST.MF file and put it inside a folder named META-INF at the root of your JAR. Inside, you can define Frame as the Main-Class:

Manifest-Version: 1.0
Main-Class: my.company.app.Frame

Eclipse can do this step automatically if you select export->Runnable Jar.

这篇关于使用Eclipse从类和图像创建可执行的JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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