简体   繁体   English

如何在Eclipse中制作可执行的JAR应用程序?

[英]How to make executable JAR app in Eclipse?

I've made some Java application, and I want to make executable JAR file. 我做了一些Java应用程序,我想制作可执行的JAR文件。 I've tried to use "Export"->"Runnable JAR file", and it works, but my application contains folders with images, and application executes without any images! 我尝试使用“导出” - >“Runnable JAR文件”,它可以工作,但我的应用程序包含带有图像的文件夹,应用程序执行时没有任何图像! But if I try to use "Export"->"JAR file", I make JAR file, but I can't execute it - click by file and get nothing! 但是,如果我尝试使用“导出” - >“JAR文件”,我会创建JAR文件,但我无法执行它 - 按文件单击并且什么也得不到! I choose main class, any resources, but my JAR file doesn't execute. 我选择主类,任何资源,但我的JAR文件不执行。 Please, tell me, how can I fix it? 请告诉我,我该如何解决?

It all depends on how the images are loaded/used in your Java code. 这完全取决于如何在Java代码中加载/使用图像。 What works best for me, is to have a folder (package) in your project called something appropriate (eg "res" or "resources") and put everything that is not java code in there. 对我来说最有用的是在项目中有一个文件夹(包),称为适当的东西(例如“res”或“resources”),并将那些不是java代码的东西放在那里。 Then you can load your resources using URLs that point to these resources. 然后,您可以使用指向这些资源的URL加载资源。 See example below: 见下面的例子:

URL css_url = getClass().getResource("/resource/style.css");

In order for the application to know where are the extra contents are (media, classes etc) it needs a manifest. 为了让应用程序知道额外内容在哪里(媒体,类等),它需要一个清单。 The manifest should have been generated along with your eclipse setup, if it is you will need to edit it and input some paths. 清单应该与您的eclipse设置一起生成,如果是,您将需要编辑它并输入一些路径。

If you dont have one you can simply create one. 如果你没有,你可以简单地创建一个。

Everything you need to know about manifests and how to do you can find here: 您需要了解有关清单以及如何操作的所有信息,请访问:

http://docs.oracle.com/javase/tutorial/deployment/jar/index.html http://docs.oracle.com/javase/tutorial/deployment/jar/index.html

Add img folder inside src folder of your project. 在项目的src文件夹中添加img文件夹。 Refresh the project. 刷新项目。 Export again to Executable Jar File 再次导出到可执行文件夹文件

Your JAR works fine, problem is in location of your images. 您的JAR工作正常,问题在于您的图像位置。 You could create a new folder like: images inside of your project and put your icons in there. 您可以创建一个新文件夹,例如:项目内部的images ,并将图标放在那里。 You will use this way to set icon on your menu: 您将使用这种方式在菜单上设置图标:

nameOfComponent.setIcon(new ImageIcon("images/NameOfIcon.PNG"));

But the problem in this aproach is that you would have to "carry" your images folder with your JAR file: 但是这个方法的问题是你必须用你的JAR文件“携带”你的images文件夹:

在此输入图像描述

Thats why you should consider this aproach 这就是为什么你应该考虑的aproach

You will get something like this in your Project Explorer: 您将在Project Explorer中获得类似的内容:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM