使用JSch的NoClassDefFoundError SecretKeySpec

时间:2017-07-24 09:45:19

标签: java jsch

我使用以下方法连接远程主机并使用JSch执行命令:

public String runCommandOnRemote(String username, String password, String hostname, String command) {
    String output = null;
    JSch jsch = new JSch();
    java.util.Properties config = new java.util.Properties();
    config.put(Constants.STRICT_HOST_KEY_CHECKING, Constants.STRICT_HOST_KEY_CHECKING_NO);
    Session session;
    try {
        session = jsch.getSession(username, hostname);
        session.setPassword(password);
        session.setConfig(config);
        session.connect(30000);
        ChannelExec channel = (ChannelExec) session.openChannel(Constants.CHANNEL_EXEC);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
        ((ChannelExec) channel).setCommand(command);
        channel.setCommand(command);
        channel.connect(3 * 1000);
        while ((output = bufferedReader.readLine()) != null) {
        }
        channel.disconnect();
        session.disconnect();
        return output;
    } catch (JSchException | IOException e) {
        new InstanceException("Error connecting to remote host" + hostname + ". " + e.getMessage());
        logger.error("Error connecting to remote host " + hostname + "\n" + e.getMessage());
        e.printStackTrace();
    }

在session.connect()上,它给出了以下错误:

[err] Exception in thread "Thread-57" 
[err] java.lang.NoClassDefFoundError: javax/crypto/spec/SecretKeySpec
[err]   at com.jcraft.jsch.jce.AES256CTR.init(AES256CTR.java:55)
[err]   at com.jcraft.jsch.Session.checkCipher(Session.java:2497)
[err]   at com.jcraft.jsch.Session.checkCiphers(Session.java:2474)
[err]   at com.jcraft.jsch.Session.send_kexinit(Session.java:624)
[err]   at com.jcraft.jsch.Session.connect(Session.java:307)

SecretKeySpec类作为JRE系统库的一部分捆绑在jce.jar中。那为什么我会收到这个错误?

1 个答案:

答案 0 :(得分:0)

这是一个OSGi项目,我不得不在清单文件中添加以下依赖项:javax.crypto,javax.crypto.interfaces,javax.crypto.spec

以下是我的清单文件的一部分:

Manifest-Version: x.x
Bundle-ManifestVersion: x
Bundle-Name: xxx
Bundle-SymbolicName: xxx
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Version: x.x

Import-Package: example.com,
javax.crypto,
javax.crypto.interfaces,
javax.crypto.spec
Bundle-ClassPath: .,
jsch-0.1.54.jar
Bundle-Blueprint: OSGI-INF/blueprint/*.xml