java eclipse创建可执行jar [英] java eclipse create executable jar

查看:103
本文介绍了java eclipse创建可执行jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipse来创建可执行的jar。它依赖外部其他罐子。

在Eclipse中,很简单,您只需要选择将所需的库解压缩到生成的JAR 中。

您可以创建一个可执行的jar。可以在任何安装jre的地方执行。



但是如果我使用命令行编译jar。

javac -classpath [external jars] * .java

jar cfm [a name] .jar manifest * .class [external jars]



它可以生成jar。但是jar只能在生成的目录中执行。
如果我把它放到另一个目录或机器中,它会抱怨 NoClassDefFoundError



所以,我的问题是如何使用命令行生成可执行jar作为Eclipse。

解决方案

一个jar文件不能有其依赖关系jar。在Eclipse的情况下,它将从依赖关系jar中解压所有类,并将其与您的类文件一起捆绑到单个jar中。如果没有以日食的方式,您需要



1)创建一个清单文件,列出所有依赖jar

  Manifest-Version:1.0 
主类:您的主类
类路径:dependency1.jar dependency2.jar dependency3.jar
dependency4。 jar依赖关系5.jar

2)使用包含所有依赖关系的类路径,使用类文件创建jar jars和使用上面创建的mainfest文件。



3)在同一个文件夹中,您创建了您的jar,放置所有依赖项。



现在您的文件夹将如下所示,



yourjar.jar(使用上面创建的清单文件)
dependency1.jar
dependency2.jar
dependency3.jar
dependency4.jar
dependency5.jar



4)现在,如果你想分享这个,你需要共享这个文件夹,你可以从这个文件夹启动你的jar。这是您的可执行文件夹,您可以从任何地方运行它。


I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.

But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]

It can generate jar. But the jar can only be executed in the directory where it is produced. If I put it into another directory or machine, it complains NoClassDefFoundError.

So, my question is that how I can generate executable jar using command line as Eclipse.

解决方案

A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to

1) Create a manifest file which lists all the dependency jars

Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
 dependency4.jar dependency5.jar

2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.

3) In this same folder where you created your jar, place all the dependency jars.

Now your folder will look like this,

yourjar.jar (With the manifest file you created above) dependency1.jar dependency2.jar dependency3.jar dependency4.jar dependency5.jar

4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.

这篇关于java eclipse创建可执行jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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