java 如何从类似于 eclipse 的命令行创建可运行的 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16198409/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:11:09  来源:igfitidea点击:

how to create a runnable jar file from command line similar to eclipse

javalinuxcommand-linejarexecutable-jar

提问by Satya Swaroop Boddu

I understood that eclipse generates a runnable jar file with all library .jar files extracted and included in the jar file. How do I do the similar this while creating a jar file manually from command prompt. Do I need to extract all the lib jars into classes folder?

我知道 eclipse 会生成一个可运行的 jar 文件,其中包含提取并包含在 jar 文件中的所有库 .jar 文件。在从命令提示符手动创建 jar 文件时,我如何执行类似的操作。我是否需要将所有 lib jar 解压缩到 classes 文件夹中?

Currently I am using -

目前我正在使用 -

    jar cvfm example.jar manifest.txt -C  Classes/ .

Which creates a jar file with all the class files and I need to have all the library jars in the same folder as this example.jar exists.

这将创建一个包含所有类文件的 jar 文件,我需要将所有库 jar 放在与此 example.jar 存在的同一文件夹中。

采纳答案by radai

there's no way do this kind of thing from the jar command line utility. there are 3 main solutions to the dependencies problem:

jar 命令行实用程序无法执行此类操作。依赖问题有 3 个主要解决方案:

  1. in your MANIFEST.MF you may reference other jar files as part of your classpath and then people could invoke your code using java -jar [just your jar]and your dependencies will be picked up
  2. you can package your code and dependencies into a fat jar (which is what youre describing). usually you just convert your project to use some build tool (more advanced than simple javac and jar) and configure the tool. for example with maven. see 2 different approaches hereand here
  3. (again with a build tool) you can use something better than a fat jar - use one-jar. the difference is that with a fat jar you run the risk of files with the same name in different libraries "colliding"
  1. 在您的 MANIFEST.MF 中,您可以引用其他 jar 文件作为类路径的一部分,然后人们可以使用调用您的代码java -jar [just your jar]并获取您的依赖项
  2. 您可以将代码和依赖项打包到一个胖 jar 中(这就是您所描述的)。通常,您只需将项目转换为使用一些构建工具(比简单的 javac 和 jar 更高级)并配置该工具。例如使用 maven在这里这里看到 2 种不同的方法
  3. (再次使用构建工具)您可以使用比胖罐更好的东西 - 使用one-jar。不同之处在于,使用胖 jar 时,您可能会面临不同库中同名文件“冲突”的风险