Below is my code,if i set the command as "ls ~/" I can get the output of 
command ls by reading the input stream from channel.

    if i set the command as "java -version", i can not get any output and the 
exitcode is 0.

    command = "ls ~/";
    //command = "java -version";
    InputStream in = null;
    InputStream err = null;
    BufferedReader inReader = null;
    BufferedReader errReader = null;
    try {
      JSch jsch = new JSch();
      Session session = jsch.getSession(user, host, port);
      session.setUserInfo(userInfo);
      session.setTimeout(timeout);
      session.connect();
      Channel channel = session.openChannel("exec");
      ((ChannelExec)channel).setCommand(command);
      channel.setInputStream(null);
      ((ChannelExec)channel).setErrStream(null);

      in = channel.getInputStream();
      err = ((ChannelExec)channel).getErrStream();

      channel.connect();

      int exitCode = 0;
      while(true) {
        if(channel.isClosed()){
          exitCode = channel.getExitStatus();
          break;
        }   
        try {
          Thread.sleep(1000);
        } catch (InterruptedException ie) {
        }   
      }   

      inReader = new BufferedReader(new InputStreamReader(in));
      errReader = new BufferedReader(new InputStreamReader(err));
   
    if (exitCode != 0) {
      while ((s = errReader.readLine()) != null) {
        sb.append(s).append("\n");
      }   
      status.setData(sb.toString());
    } else {
      while ((s = inReader.readLine()) != null) {
        sb.append(s).append("\n");
      }   
      System.out.println(sb.toString());
    }   


------------------------------------------------------------------------------
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to