带有组合库的jar文件中的Class-Path

时间:2021-04-24 06:28:08

I am trying to create a jar file that is "self contained" concerning the libraries it needs.

我正在尝试创建一个关于它需要的库“自包含”的jar文件。

Therefore, I created the following ant file:

因此,我创建了以下ant文件:

<project name="srv" default="prod">
    <target name="prod">
        <jar destfile="build/ServerApplication.jar" basedir="bin/">
            <restrict>
             <name name="**/*.class"/>
             <archives>
               <zips>
                 <fileset dir="lib/" includes="**/*.jar"/>
               </zips>
             </archives>
            </restrict>
            <manifest>
                <attribute name="Class-Path" value="." />
                <attribute name="Main-Class" value="my.package.ServerApplication" />
            </manifest>
        </jar>
    </target>
</project>

But, when I try to launch the application using

但是,当我尝试使用启动应用程序时

> java -jar ServerApplication.jar

I get an error

我收到一个错误

No suitable driver found for jdbc:mysql://localhost/db?user=root&password=

没有为jdbc找到合适的驱动程序:mysql:// localhost / db?user = root&password =

I reckon this is because the com.mysql.jdbc package inside the jar can't be found. Why is this? When I set the Class-Path to ., shouldn't the jar be able to find all classes inside that jar?

我估计这是因为无法找到jar中的com.mysql.jdbc包。为什么是这样?当我将Class-Path设置为。时,jar是否应该能够找到该jar中的所有类?

I opened the jar in a decompiler, and all the classes are where they need to be, see here:

我在一个反编译器中打开了jar,所有的类都是他们需要的地方,请看这里:

带有组合库的jar文件中的Class-Path

What is going wrong here? My MANIFEST looks like this:

这里出了什么问题?我的MANIFEST看起来像这样:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_07-b10 (Oracle Corporation)
Main-Class: my.package.ServerApplication
Class-Path: .

3 个解决方案

#1


0  

You must explicitly write the names of the jars if you want them included separated by spaces. Otherwise I would recommend simply launching your jar and providing the class path as a parameter manually:

如果要将它们包含在空格中,则必须显式写入jar的名称。否则,我建议您只需启动jar并手动将类路径作为参数提供:

java -cp . -jar ServerApplication.jar

See here for more information.

浏览此处获取更多信息。

#2


0  

I think you should avoid setting classpath at all, and since you are packaging everything in uber-jar - it should work.

我认为你应该完全避免设置类路径,因为你将所有内容打包在uber-jar中 - 它应该可行。

#3


0  

Maybe you could try the excellent eclipse plugin which is fatjar. It had used it many times for including all dependencies of Java Swing based applications.

也许你可以尝试一下胖子的优秀eclipse插件。它已多次使用它来包含基于Java Swing的应用程序的所有依赖项。

#1


0  

You must explicitly write the names of the jars if you want them included separated by spaces. Otherwise I would recommend simply launching your jar and providing the class path as a parameter manually:

如果要将它们包含在空格中,则必须显式写入jar的名称。否则,我建议您只需启动jar并手动将类路径作为参数提供:

java -cp . -jar ServerApplication.jar

See here for more information.

浏览此处获取更多信息。

#2


0  

I think you should avoid setting classpath at all, and since you are packaging everything in uber-jar - it should work.

我认为你应该完全避免设置类路径,因为你将所有内容打包在uber-jar中 - 它应该可行。

#3


0  

Maybe you could try the excellent eclipse plugin which is fatjar. It had used it many times for including all dependencies of Java Swing based applications.

也许你可以尝试一下胖子的优秀eclipse插件。它已多次使用它来包含基于Java Swing的应用程序的所有依赖项。