具有依赖性的Maven可执行jar提供NoClassDefFound异常

时间:2018-04-23 08:22:57

标签: maven jar dependencies

我正在尝试运行由maven构建的依赖关系的jar.Dependency安装在本地存储库中,只是将依赖项添加到pom.xml ..它已成功编译但在运行时获取ClassNotFound异常。我在互联网上探索但没有运气

POM.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xyz</groupId>
  <artifactId>XYZ</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Test</name>
  <url>http://maven.apache.org</url>
  <build>
        <resources>
            <resource>
              <directory>WEB-INF/classes/</directory>
            </resource>
        </resources> 
         <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.xyz.Abc</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    </plugins>
    </build>
    <repositories>
      <repository>
         <id>localrepository</id>
         <url>${user.home}/.m2/repository</url>
      </repository>
   </repositories>      
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency> 
    <dependency>    
        <groupId>com.abc</groupId>  
        <artifactId>jarname</artifactId>    
        <version>1.0</version>
        <scope>compile</scope>
    </dependency> 
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

要创建一个“随处”运行的jar,可以创建一个可执行jar:

How can I create an executable JAR with dependencies using Maven?

否则,您需要小心类路径,否则Java将无法找到您的依赖项。