Always get -1 exit status

  • Barry Marcus

    Barry Marcus - 2008-05-03


    The method I wrote to execute commands on my linux machine always returns an exit status of -1.  It makes no difference what the command is.  I am a complete newbie to the Jsch package, so I'm at a complete loss to figure out what's going on.  Hopefully this is a very simple issue...  Any help would be appreciated.  Thanks in advance.

    I have pasted the method below.


    public int executeCommand(String[] args, ConnectionInfo userInfo) {   
        int exitStatus = 0;
        if(args.length == 0) {return exitStatus = 1000;}
        if(userInfo == null) {return exitStatus = 1001;}
        if(userInfo.validateWithPassword() != 0) {return exitStatus = 1002;}

        try {
          String user = userInfo.getUserName();
          String host = userInfo.getHostName();
          int portNumber = userInfo.getPortNumber();
          //args[0] will hold the path to the or just the command to execute itself
          StringBuffer command = new StringBuffer();
          //Get the possible command params to add on to the command to execute
          int argsLenth = args.length;
          while (argsLenth > 1) {
              int index = argsLenth -1;
              command.append(" ");
              argsLenth --;
          JSch jsch = new JSch();
          Session session = jsch.getSession(user, host, portNumber);
          // username and password will be given via UserInfo interface.

          Channel channel=session.openChannel("exec");

          exitStatus = channel.getExitStatus();
        } catch(Exception e){
          System.out.println("Exception - Exit Status = " + exitStatus + "\nException: " + e.toString());
          return exitStatus;
        return exitStatus;

    • Ray

      Ray - 2008-05-29

      I'm having the same problem. I am using the ant SSHExec task and the task always fails because of exit code 1. I set up a project to debug this through my IDE and it seems to me that there are two lines in that could set the exit code. Neither of these are ever run. Please, any thoughts on this?


    • cytoe

      cytoe - 2008-07-08

      You need to loop until the command completes. In the loop you can do:

      if (channel.isClosed())   {

      i.e. the channel has to be closed before getExitStatus() is called.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks