在资源文件夹中将JAR添加到Maven中的类路径

时间:2018-06-20 09:08:17

标签: java maven jar classpath

我在resources文件夹中有两个jar,我使用maven将项目构建到jar(例如editor.jar)。 我的类File在构建时不依赖于此jar。

但是当我使用命令java -jar editor.jar运行editor.jar时 它需要lib文件夹中的任何jar项作为依赖项。

所以我需要将它们添加到jar到classpath中。我在question的同一网站上阅读过:但这对我不起作用。

我的目录如下:

到目前为止,我在pom.xml中做了什么:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>editor</groupId>
    <artifactId>editor</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <finalName>editor</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Jdbc</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/*.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

如何将两个jar添加到classpath并执行editor.jar?

1 个答案:

答案 0 :(得分:0)

尝试在您的pom.xml文件中添加<Class-Path>resources/lib/*.jar</Class-Path>