具有依赖性的可执行Jar

具有依赖性的可执行Jar

本文介绍了具有依赖性的可执行Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令行构建一个可执行jar(我不想使用ant或OneJar)。这是我的file.jar的内容:

I'm trying to build an executable jar from the command line (I don't want to use ant or OneJar ) . Here is the content of my file.jar:

jar tvf file.jar
     0 Mon Sep 20 17:16:12 CEST 2010 lib/
 45396 Mon Sep 20 17:16:12 CEST 2010 lib/org.apache.commons.logging_1.0.4.v201005080501.jar
321330 Mon Sep 20 17:16:12 CEST 2010 lib/org.apache.commons.httpclient_3.1.0.v201005080502.jar
 55003 Mon Sep 20 17:16:12 CEST 2010 lib/org.apache.commons.codec_1.3.0.v20100518-1140.jar
     0 Mon Sep 20 19:15:00 CEST 2010 META-INF/
   265 Mon Sep 20 19:12:44 CEST 2010 META-INF/MANIFEST.MF
530609 Mon Sep 20 17:16:12 CEST 2010 ped.jar

META-INF / MANIFEST.MF的内容:

the content of META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Class-Path: ped.jar lib/org.apache.commons.codec_1.3.0.v20100518-1140.jar lib/org.apache.commons.httpclient_3.1.0.v201005080502.jar lib/org.apache.commons.logging_1.0.4.v201005080501.jar
Main-Class: fr.inserm.umr915.bomcat.ped.PedigreeDrawer

和p ed.jar do包含Main-Class

and the ped.jar do contains the Main-Class

jar tvf ped.jar | grep PedigreeDrawer.class
 39541 Mon Sep 20 17:16:10 CEST 2010 fr/inserm/umr915/bomcat/ped/PedigreeDrawer.class

但是当我试图执行file.jar时,我收到了一个错误:

but when I'm trying to execute the file.jar, I got an error:

java -jar file.jar
Exception in thread "main" java.lang.NoClassDefFoundError: fr/inserm/umr915/bomcat/ped/PedigreeDrawer
Caused by: java.lang.ClassNotFoundException: fr.inserm.umr915.bomcat.ped.PedigreeDrawer
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

所以,我在这里遗漏了一些东西。我不能通过包含依赖项来打包可执行jar吗?

So, I'm missing something here. Can't I package an executable jar by just including the dependencies ?

感谢您的帮助

Pierre

推荐答案

如前所述, Class-Path:属性中的jar是相对于jar文件的loaction而不是jar文件。这可能是Java用于运行applet的日子里的残余。

As already mentioned the jars in the Class-Path: attribute are relative to the loaction of the jar file and not inside the jar file. This is probably a remnant from the days Java was meant to run applets.

你需要使用像maven uberjar这样的工具来一起破解所有需要的类用于创建独立可执行jar的jar。

You'll need to use a tool like maven uberjar to clobber all needed classes together in a single jar to create a standalone executable jar.

您当然也可以将所有jar解压缩到一个目录中,添加META-INF并清单并再次压缩所有内容你绝对想避免使用工具。

You can of course also just unzip all jars in a single directory, add the META-INF and manifest and zip everything up again if you absolutely want to avoid using tools.

这篇关于具有依赖性的可执行Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:50