Maven汇编:NoClassDefFoundError

时间:2016-11-30 07:28:15

标签: java maven pdfbox

我正在使用maven程序集插件构建一个JAR,但是在运行以下内容后出现此错误

java -jar target/pdfbox-printing-1.0-SNAPSHOT-jar-with-dependencies.jar

错误是

Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider

这就是我编译JAR的方法

mvn clean compile assembly:single

这是我的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.company.printing</groupId>
  <artifactId>pdfbox-printing</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>pdfbox-printing</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.company.printing.App</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

我实际上正在尝试构建一个使用apache pdfbox的项目。我认为maven程序集插件会将所有依赖项捆绑在一个JAR中,但为什么我会收到此错误,如果它是真的。

1 个答案:

答案 0 :(得分:1)

如错误所示,NoClassDefFoundErrororg/bouncycastle/jce/provider/BouncyCastleProvider,在这种情况下,您可以通过在<dependencies>内添加以下内容来使用maven库:

<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk16 -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.54</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcmail-jdk15on</artifactId>
    <version>1.54</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpkix-jdk15on</artifactId>
    <version>1.54</version>
</dependency>

这将帮助您导入包并使用代码中所需的类。

可能需要更多依赖项,请参阅here