When I create a jar file in a subdirectory, the BouncyCastleProvider
class from bcprov-jdk15on-159.jar fails to load with a ClassNotFoundException
. I would think that the location where a jar file is created should have no impact on its contents and behavior.
Here is the example of creating a working jar.
$ jar cfm MyProject.jar Manifest.txt Main.class bcprov-jdk15on-159.jar
$ java -jar MyProject.jar
hello provider: BC version 1.59
And here is the example where running jar with exactly the same input files, but a different jar file destination, results in a failing jar.
$ jar cfm dist/MyProject.jar Manifest.txt Main.class bcprov-jdk15on-159.jar
$ java -jar dist/MyProject.jar
Error: Unable to initialize main class Main
Caused by: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
This is the file Manifest.txt
Manifest-Version: 1.0
Main-Class: Main
Class-Path: bcprov-jdk15on-159.jar
and this is the Main.java
file that uses the BouncyCastleProvider
class.
public class Main {
public static void main(String... arg) {
java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
java.security.Provider p = java.security.Security.getProvider("BC");
System.out.println("hello provider: " + p);
}
}
I see this behavior both with JDK 8 and with JDK 9, and also both with the JDK jar command (shown above) and with Ant's jar task.
I stumbled on this problem while trying to upgrade the PCSecrets password manager to work under Java 9.