如何从命令行创建一个类似于eclipse的可运行jar文件 [英] how to create a runnable jar file from command line similar to eclipse

查看:125
本文介绍了如何从命令行创建一个类似于eclipse的可运行jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

目前我正在使用 -

Currently I am using -

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

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

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命令行实用程序无法做到这一点。依赖项问题有3个主要解决方案:

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


  1. 在您的MANIFEST.MF中,您可以引用其他jar文件作为类路径的一部分,然后人们可以使用 java -jar [只是你的jar] 调用你的代码,你的依赖关系将被选中

  2. 你可以打包你的代码和依赖到一个胖罐(这是你所描述的)。通常你只是转换你的项目使用一些构建工具(比简单的javac和jar更先进)并配置工具。例如,与maven 。请参阅两种不同的方法此处此处

  3. (再次使用构建工具)您可以使用比胖罐更好的东西 - 使用 one-jar 。不同之处在于,使用胖罐会冒不同库中具有相同名称的文件的风险碰撞

  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 here and 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"

这篇关于如何从命令行创建一个类似于eclipse的可运行jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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