Running Linux commands on Java through JSch

后端 未结 2 1302
难免孤独
难免孤独 2021-01-03 06:13

I\'m establishing an SSH connection through JSch on Java and everything seemed to be working fine, until I tried to run this .sh file. The shell script\'s name is repo

2条回答
  •  独厮守ぢ
    2021-01-03 06:51

    I suspect that your shell command is erroring out for some reason - perhaps svn isn't on your path, perhaps there are other weird environmental effects - but you're not getting the error output because you aren't looking for it. Normally, errors are sent on the stream you get from channelExec.getErrStream but in your code you only read from the getOutputStream stream.

    To diagnose this, you're going to need to get those error messages. It's probably easier to get linux to use one stream for both regular output and error messages than to have your java program pulling from two streams at once, so I'd add this line as the top line of repoUpdate.sh:

    exec 2>&1
    

    That'll then cause the rest of the script to use the one stream you're reading from as both output and error.

    Also, right before you call chanelExec.disconnect, in your java program you should record the exit status, and then change your "Done!" message based on what it was:

        int exitStatus = channelExec.getExitStatus();
        channelExec.disconnect();
        session.disconnect();
    
        if (exitStatus < 0) {
            System.out.println("Done, but exit status not set!");
        } else if (exitStatus > 0) {
            System.out.println("Done, but with error!");
        } else {
            System.out.println("Done!");
        }
    

    If you do this, you should then get error messages that tell you why your command isn't working as you expect it should.

提交回复
热议问题