Search code examples
javaclasspathmaven-3bouncycastle

Bouncy Castle not found in classpath, but works when run in maven


This is a very weird error, that I don't know more about how to fix it.

I'm using Bouncy Castle libs to decrypt a password. I configured it in my maven pom.xml as:

<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15</artifactId>
  <version>1.46</version>
  <scope>provided</scope>
</dependency>

The provided scope is because it is a signed jar. It is a command-line application and I use maven-assembly-plugin to generate a jar with all the dependencies included. I can't include it because, as a signed jar, it would break the signature verification. Marking it as provided it isn't included in the generated jar.

So I try to pass it in the classpath with the command:

java -cp bcprov-jdk15-1.46.jar -jar my_executable.jar 

but when I run, I get a Class not found exception:

java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider

I've tried dozens of ways to run it, but always get the same error.

But what is really weird is that my unit tests that use the library run fine. If I type mvn test, the exact code that fails when I run from the command-line works fine. If I go to my ~/.m2 repo and rename the bcprov-jdk15-1.46.jar, the tests fail with the same Class Not Found Error.

Why my code can run from inside maven, but fails from a jar with dependencies file? I'm really lost.


Solution

  • I solved it putting the Bouncy Castle jar inside the Java installation in $JAVA_HOME\lib\ext\