且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Eclipse从类和图像创建可执行的JAR

更新时间:2023-01-10 13:17:36

我认为***把你的照片放在一个包里。这样,所有的东西都将从您的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.