我正在使用SSHJ在远程服务器上执行命令,如下所示进行连接:

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);
    }
}

当我在IDE中运行它时,它运行没有问题。但是,当我尝试使用Gradle在Jenkins环境中运行它时,得到以下堆栈跟踪:
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

我尝试将bouncycaSTLe添加为单独的依赖项(包括bcprov和bcpkix软件包),即使SSHJ引入了它,我也考虑了多个版本,并尝试了启用和禁用Jenkins bouncycaSTLe插件。我也尝试过使用schmizz SSHJ代替Hierynomus。

我正在使用Java 8,Gradle 3.2.1(原因),Jenkins 2.190.3和Hierynomus SSHJ 0.27。

最佳答案

我发现了问题。我要导入的另一个依赖项是悄悄地引入了较旧版本的bouncycaSTLe,该版本覆盖了SSHJ带来的一个依赖项。我排除了该依赖项并解决了问题。

09-25 20:37