0

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.

bazi
  • 1,758
  • 5
  • 27
  • 41
  • BouncyCastle is an *optional* dependency (it is only needed if you have to deal with encrypted or signed PDFs); probably the `maven-assembly-plugin` does not include optional dependencies? – mkl Nov 30 '16 at 07:42
  • does the mvn compile executes successfully? – Naman Nov 30 '16 at 07:54
  • could you check where in the code `org.bouncycastle.jce.provider.BouncyCastleProvider` is used. and if the code compiles which dependency is bringing the class in – Naman Nov 30 '16 at 07:56
  • If u still want **BouncyCastle** to get added in the single jar, please download this jar and provide it as an exernal library. This will surly work out then. – dildeepak Nov 30 '16 at 07:56
  • 1
    also if you need the package, you can probably add it as an dependency in your pom. – Naman Nov 30 '16 at 07:56
  • ...in particular, add it as a non-*optional* dependency. – mkl Nov 30 '16 at 08:00
  • 2.0.0 is an outdated version. 2.0.3 is the current version. At in 2 weeks it will be 2.0.4. – Tilman Hausherr Nov 30 '16 at 10:29
  • Even though the BouncyCastle dependency is shown as optional, PDFBox had references to it hardcoded in the source code. This was a bug solved in 2.0.7 https://issues.apache.org/jira/browse/PDFBOX-3831 – walen Jun 04 '18 at 13:11

1 Answers1

1

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-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>

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

More dependencies may be needed, see here.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    1.45 is not the correct version for 2.0. See here: https://pdfbox.apache.org/2.0/dependencies.html – Tilman Hausherr Nov 30 '16 at 10:29
  • @TilmanHausherr please go ahead and edit the post adding the updated version if you have used it. I just made sure for the class required has the correct dependency included. – Naman Nov 30 '16 at 16:18