gpt4 book ai didi

java - jar 内的引用 jar

转载 作者:IT老高 更新时间:2023-10-28 20:29:08 25 4
gpt4 key购买 nike

我有一个jar,内容如下图,

enter image description here

下面是我的 list 文件

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_06-b24 (Oracle Corporation)
Main-Class: org.sai.com.DerbyDemo
Class-Path: derby.jar derbyclient.jar derbynet.jar derbytools.jar

当我尝试运行 jar 时,它抛出了一个 ClassNotFoundExcception 意味着它没有引用外部 jar 中的 jar。

Class-Path 属性中,如何在实际 jar 中引用 jars(derby.jar 等)?

最佳答案

你需要一个自定义的类加载器,看看 One Jar .

One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.

它有一个 Ant 任务,它也可以简化它的构建。

引用(来自 background)

Most developers reasonably assume that putting a dependency Jar file into their own Jar file, and adding a Class-Path attribute to the META-INF/MANIFEST will do the trick:


jarname.jar
| /META-INF
| | MANIFEST.MF
| | Main-Class: com.mydomain.mypackage.Main
| | Class-Path: commons-logging.jar
| /com/mydomain/mypackage
| | Main.class
| commons-logging.jar

Unfortunately this is does not work. The Java Launcher$AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end. This approach will only work if you install (i.e. scatter) the supporting Jar files into the directory where the jarname.jar file is installed.

关于java - jar 内的引用 jar ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12357136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com