简体   繁体   中英

NoClassDefFoundError: org/bouncycastle/openssl/PEMParser when using SSHJ on Jenkins

I'm using SSHJ to execute commands on a remote server, connecting like so:

private static void connect() {
    try {
        client = new SSHClient();
        client.addHostKeyVerifier(new PromiscuousVerifier());
        client.connect(HOST);
        String path = System.getProperty("/**/user.home") + "/.ssh/id_rsa";
        client.authPublickey(USER, client.loadKeys(path));
    } catch (IOException e) {
        logErrorAndRethrowException("Error connecting to server via SSH", e);
    }
}

When I run this in my IDE, it runs without an issue. However, when I try to run it on a Jenkins environment using Gradle, I get the following stack trace:

Exception in thread "reader" java.lang.NoClassDefFoundError: org/bouncycastle/openssl/PEMParser
    at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.readKeyPair(PKCS8KeyFile.java:131)
    at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.getPrivate(PKCS8KeyFile.java:72)
    at net.schmizz.sshj.userauth.method.KeyedAuthMethod.putSig(KeyedAuthMethod.java:59)
    at net.schmizz.sshj.userauth.method.AuthPublickey.sendSignedReq(AuthPublickey.java:74)
    at net.schmizz.sshj.userauth.method.AuthPublickey.handle(AuthPublickey.java:45)
    at net.schmizz.sshj.userauth.UserAuthImpl.handle(UserAuthImpl.java:143)
    at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:511)
    at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:107)
    at net.schmizz.sshj.transport.Decoder.received(Decoder.java:175)
    at net.schmizz.sshj.transport.Reader.run(Reader.java:60)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.openssl.PEMParser
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 10 more

I've tried adding bouncycastle as a separate dependency (both the bcprov and bcpkix packages) even though SSHJ brings it in, I've mucked around a bunch with versions, I've tried with the Jenkins bouncycastle plugin both enabled and disabled. I've also tried with the schmizz SSHJ instead of Hierynomus.

I'm using Java 8, Gradle 3.2.1 (cause reasons), Jenkins 2.190.3, and Hierynomus SSHJ 0.27.

I found the problem. Another dependency I was importing was quietly bringing in an older version of bouncycastle, which was overriding the one SSHJ brings in. I excluded that dependency and solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM