在 Eclipse 中生成一个包含外部库的 Jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7986063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating a Jar in Eclipse including external library
提问by arcy
I have a java application I've written in eclipse. It works fine there.
我有一个用 Eclipse 编写的 Java 应用程序。它在那里工作正常。
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. Is that possible?
我想要一种方法来使用 eclipse 生成必要的文件来打包这个应用程序以在另一台机器上运行。那可能吗?
If I choose "create executable jar file", it creates this huge file; 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. More annoying is the fact that the executable jar file option puts lots of files from my eclipse project into that jar file. 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 文件”,它会创建这个巨大的文件;它确实解包并重新打包这两个库,我知道这是包含其功能的一种方法。如果它们以某种方式被保留为自己的罐子,我实际上更喜欢它,但我不确定 eclipse 可以做到这一点。更令人讨厌的是,可执行 jar 文件选项将我的 eclipse 项目中的大量文件放入该 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. Perhaps there is no place to include them where they could be used.
如果我选择“创建 jar”(“创建可执行 jar”的插入),我看不到在哪里可以选择在任何地方包含这两个 jar 文件。也许没有地方可以将它们包含在可以使用的地方。
If possible, I do not want to use Ant, I do not want to use Maven, I do not want to download another tool. 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.
如果可能,我不想使用 Ant,我不想使用 Maven,我不想下载其他工具。在我看来,Eclipse 已经拥有所有这些信息,我怀疑它已经可以做到这一点,而无需去学习另一个“漂亮”的工具。
回答by aleroot
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脚本以备日后修改或使用...
回答by Stephan
You actually should use Ant or Maven for your task, I see no other option. Ant is already packed with eclipse, you only need to install a JDK, not only a JRE.
您实际上应该使用 Ant 或 Maven 来完成您的任务,我看不出其他选择。Ant已经打包了eclipse,你只需要安装一个JDK,而不是一个JRE。
Ant is very easy to learn and you can find billions of examples in the internet. With ant you can do exactly what you want.
Ant 非常容易学习,您可以在互联网上找到数十亿个示例。使用 ant,您可以完全按照自己的意愿行事。
Maven is the more up-to-date way to build and package jars and do much more other stuff. Maven also is a good choice for you.
Maven 是构建和打包 jar 以及做更多其他事情的最新方法。Maven 也是您不错的选择。
回答by dmcnelis
I'll second a vote for Maven. Eclipse has a decent maven integration (m2eclipse). Then check out this answer for building the jar effectively using Maven2
我将支持 Maven 一票。Eclipse 有一个不错的 maven 集成 (m2eclipse)。然后查看此答案以使用 Maven2 有效构建 jar