bazi bazi - 9 days ago 5
Java Question

Maven Assembly : NoClassDefFoundError

I am using the maven assembly plugin to build a single JAR but I'm getting this error after running the following

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


the error is

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


this is how i compile the JAR

mvn clean compile assembly:single


here is my
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>


i am actually trying to build a project that uses apache pdfbox. and i thought the maven assembly plugin will bundle all the dependencies in one JAR but why am i getting this error if its true.

Answer

As the error reads, there is NoClassDefFoundError for org/bouncycastle/jce/provider/BouncyCastleProvider in which case you can use the maven library for the same by adding the following inside your <dependencies> :

<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk16 -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
    <version>1.45</version>
</dependency>

This shall help you import the package and use the Class required in your code.