GroovyCastException:无法将类'java.lang.Class'的对象'class com.jcraft.jsch.ChannelExec'强制转换为类

时间:2018-05-04 07:34:47

标签: java groovy jsch groovyshell

我正在尝试连接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();
    }`

1 个答案:

答案 0 :(得分:1)

问题出在这里

ChannelExec channelExec = (ChannelExec) 
session.openChannel("exec");

换行符告诉groovy解析器这两个语句。这相当于以下Java代码:

ChannelExec channelExec = (ChannelExec.class);
session.openChannel("exec");

请注意,类的名称(此处为ChannelExec)在Groovy中成为Class-Literal,而在Java中,您需要添加.class,例如ChannelExec.class

你想要的是这个:

ChannelExec channelExec = (ChannelExec) session.openChannel("exec")
相关问题