GroovyCastException:无法将对象 'class com.jcraft.jsch.ChannelExec' 与 class 'java.lang.Class' 转换为 class
GroovyCastException: Cannot cast object 'class com.jcraft.jsch.ChannelExec' with class 'java.lang.Class' to class
我正在尝试连接 linux
机器并执行名为“myscript.sh
”的 shell 脚本。虽然 运行 它,但我在 Java.
中工作正常时遇到强制转换异常
我遇到以下错误:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class com.jcraft.jsch.ChannelExec' with class 'java.lang.Class' to class 'com.jcraft.jsch.ChannelExec' error at line: 29
下面是代码片段:
`import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
JSch jsch = new JSch();
Session session;
try {
// Open a Session to remote SSH server and Connect.
// Set User and IP of the remote host and SSH port.
session = jsch.getSession("user", "host", 22);
// When we do SSH to a remote host for the 1st time or if key at the remote host
// changes, we will be prompted to confirm the authenticity of remote host.
// This check feature is controlled by StrictHostKeyChecking ssh parameter.
// By default StrictHostKeyChecking is set to yes as a security measure.
session.setConfig("StrictHostKeyChecking", "no");
//Set password
session.setPassword("Pwd");
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.connect();
// create the execution channel over the session
ChannelExec channelExec = (ChannelExec)
session.openChannel("exec");
// Set the command to execute on the channel and ex ecute the command
channelExec.setCommand("sh myscript.sh");
channelExec.connect();
// Get an InputStream from this channel and read messages, generated
// by the executing command, from the remote side.
InputStream ab = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ab));
String line;
while ((line = reader.readLine()) != null) {
log.info(line);
}
// Command execution completed here.
// Retrieve the exit status of the executed command
int exitStatus = channelExec.getExitStatus();
if (exitStatus > 0) {
log.info("Remote script exec error! " + exitStatus);
}
//Disconnect the Session
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}`
问题就在这里
ChannelExec channelExec = (ChannelExec)
session.openChannel("exec");
换行符告诉 groovy 解析器这是两个语句。这等效于以下 Java 代码:
ChannelExec channelExec = (ChannelExec.class);
session.openChannel("exec");
请注意,class(此处为 ChannelExec
)的名称在 Groovy 中变为 Class-文字,而在 Java 中,您需要添加 .class
,例如ChannelExec.class
你要的是这个:
ChannelExec channelExec = (ChannelExec) session.openChannel("exec")
我正在尝试连接 linux
机器并执行名为“myscript.sh
”的 shell 脚本。虽然 运行 它,但我在 Java.
我遇到以下错误:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class com.jcraft.jsch.ChannelExec' with class 'java.lang.Class' to class 'com.jcraft.jsch.ChannelExec' error at line: 29
下面是代码片段:
`import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
JSch jsch = new JSch();
Session session;
try {
// Open a Session to remote SSH server and Connect.
// Set User and IP of the remote host and SSH port.
session = jsch.getSession("user", "host", 22);
// When we do SSH to a remote host for the 1st time or if key at the remote host
// changes, we will be prompted to confirm the authenticity of remote host.
// This check feature is controlled by StrictHostKeyChecking ssh parameter.
// By default StrictHostKeyChecking is set to yes as a security measure.
session.setConfig("StrictHostKeyChecking", "no");
//Set password
session.setPassword("Pwd");
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.connect();
// create the execution channel over the session
ChannelExec channelExec = (ChannelExec)
session.openChannel("exec");
// Set the command to execute on the channel and ex ecute the command
channelExec.setCommand("sh myscript.sh");
channelExec.connect();
// Get an InputStream from this channel and read messages, generated
// by the executing command, from the remote side.
InputStream ab = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ab));
String line;
while ((line = reader.readLine()) != null) {
log.info(line);
}
// Command execution completed here.
// Retrieve the exit status of the executed command
int exitStatus = channelExec.getExitStatus();
if (exitStatus > 0) {
log.info("Remote script exec error! " + exitStatus);
}
//Disconnect the Session
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}`
问题就在这里
ChannelExec channelExec = (ChannelExec)
session.openChannel("exec");
换行符告诉 groovy 解析器这是两个语句。这等效于以下 Java 代码:
ChannelExec channelExec = (ChannelExec.class);
session.openChannel("exec");
请注意,class(此处为 ChannelExec
)的名称在 Groovy 中变为 Class-文字,而在 Java 中,您需要添加 .class
,例如ChannelExec.class
你要的是这个:
ChannelExec channelExec = (ChannelExec) session.openChannel("exec")