简体   繁体   English

在Eclipse中生成Jar包括外部库

[英]Generating a Jar in Eclipse including external library

I have a java application I've written in eclipse. 我有一个我用eclipse编写的java应用程序。 It works fine there. 它在那里工作正常。

It works fine run from a command line in the directory where I export it to. 它可以从我导出它的目录中的命令行运行。 In that directory is another directory containing two jar files that I need for the application, and the manifest file has a Class-path option specifying them. 在该目录中是另一个目录,其中包含应用程序所需的两个jar文件,清单文件具有指定它们的Class-path选项。

I want a way to use eclipse to generate the necessary file(s) to package this application to run on another machine. 我想要一种方法来使用eclipse生成必要的文件来打包这个应用程序以在另一台机器上运行。 Is that possible? 那可能吗?

If I choose "create executable jar file", it creates this huge file; 如果我选择“创建可执行jar文件”,它会创建这个巨大的文件; it does unpack and repack the two libraries, which I know is one way to get their functionality included. 它解压缩并重新打包两个库,我知道这是一种获取其功能的方法。 I would actually prefer it if they were left as their own jars somehow, but I am not certain eclipse can do that. 如果他们以某种方式留下自己的罐子,我会更喜欢它,但我不确定eclipse能做到这一点。 More annoying is the fact that the executable jar file option puts lots of files from my eclipse project into that jar file. 令人讨厌的是,可执行jar文件选项将我的eclipse项目中的大量文件放入该jar文件中。 I don't see an option to choose what gets included there, though I do see a place to enter inclusion and exclusion "rules' in the project properties. Do those apply here? Is there somewhere else I go to select what does and does not get included in the "executable jar"? 我没有看到选择包含在那里的内容的选项,虽然我确实看到了在项目属性中输入包含和排除“规则”的地方。这些适用于这里吗?还有其他地方我去选择做什么和做什么没有被包含在“可执行jar”中?

If I choose "create jar" (ins of "create executable jar"), I don't see where there's an option to include these two jar files anywhere. 如果我选择“创建jar”(“创建可执行jar”的内容),我看不到哪里可以选择将这两个jar文件包含在任何地方。 Perhaps there is no place to include them where they could be used. 也许没有地方可以将它们包含在可以使用的地方。

If possible, I do not want to use Ant, I do not want to use Maven, I do not want to download another tool. 如果可能的话,我不想使用Ant,我不想使用Maven,我不想下载其他工具。 It seems to me that Eclipse already has all this information and I suspect it can already do this without having to go and learn yet another "nifty" tool. 在我看来,Eclipse已经拥有所有这些信息,我怀疑它已经可以做到这一点,而无需去学习另一个“漂亮”的工具。

Eclipse has its own Jar export wizard for generate a runnable jar packed with required library or with the required library in a folder aside the jar. Eclipse有自己的Jar导出向导,用于生成一个包含所需库的可运行jar,或者在jar旁边的文件夹中包含所需的库。

Going in File ---> Export then choose Java - Runnable Jar 进入File ---> Export然后选择Java - Runnable Jar

出口

You can then choose how pack the jar and how handling libraries : 然后,您可以选择如何打包jar以及如何处理库:

罐装包装

You can also save the ant script for later modification or use ... 您还可以保存ant脚本以供以后修改或使用...

You actually should use Ant or Maven for your task, I see no other option. 你实际上应该使用Ant或Maven来完成你的任务,我没有看到别的选择。 Ant is already packed with eclipse, you only need to install a JDK, not only a JRE. Ant已经装满了eclipse,你只需要安装一个JDK,而不仅仅是一个JRE。

Ant is very easy to learn and you can find billions of examples in the internet. Ant很容易学习,你可以在互联网上找到数十亿的例子。 With ant you can do exactly what you want. 使用ant,您可以完全按照自己的意愿行事。

Maven is the more up-to-date way to build and package jars and do much more other stuff. Maven是构建和打包罐子的更新方式,还有更多其他东西。 Maven also is a good choice for you. Maven也是一个不错的选择。

I'll second a vote for Maven. 我将为Maven投票。 Eclipse has a decent maven integration (m2eclipse). Eclipse有一个不错的maven集成(m2eclipse)。 Then check out this answer for building the jar effectively using Maven2 然后查看这个答案,使用Maven2有效地构建jar

Building a runnable jar with Maven 2 用Maven 2构建一个可运行的jar

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

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