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?