2

I'm using below method to connect to remote host and execute command using 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();
    }

On session.connect(), it gives below error:

[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)

The SecretKeySpec class comes bundled in jce.jar as part of JRE System Library. Then why am i getting this error?

HyperioN
  • 3,433
  • 2
  • 22
  • 36
  • Possible duplicate of [Why am I getting package javax.crypto does not exist](https://stackoverflow.com/questions/14935447/why-am-i-getting-package-javax-crypto-does-not-exist) – araknoid Jul 24 '17 at 09:51

1 Answers1

2

This was an OSGi project and I had to add following dependency in the manifest file: javax.crypto, javax.crypto.interfaces, javax.crypto.spec

Below is part of my manifest file:

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
HyperioN
  • 3,433
  • 2
  • 22
  • 36
  • It would be helpful if you would provide your code for posterity. For example, your manifest or generator file, like a pom.xml. – Jonathan Komar Apr 03 '19 at 12:36