Running Linux commands on Java through JSch

后端 未结 2 1286
难免孤独
难免孤独 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 07:17

    So here's what I did.
    I added the exec 2>&1 at the top of the repoUpdate.sh file and added that piece of code to read the output error as @DanielMartin suggested, resulting on this:

    public void cmremove()
    {
        try
        {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            UserInfo ui = new SUserInfo(pass, null);
            session.setUserInfo(ui);
            session.setPassword(pass);
            session.connect();
    
            ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
    
            InputStream in = channelExec.getInputStream();
    
            channelExec.setCommand("./repoUpdate.sh");
            channelExec.connect();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            int index = 0;
    
            while ((line = reader.readLine()) != null)
            {
                System.out.println(++index + " : " + line);
            }
    
            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!");
            }
        }
        catch(Exception e)
        {
            System.err.println("Error: " + e);
        }
    }
    

    So that actually helped a lot. It confirmed that the command was, in fact, not executing correctly as I thought. I was getting this on my java output:

    run:
    1 :  ****Repository update****
    2 :  Location: /home/cissys/repo/
    3 :  Command: svn update /home/cissys/repo/2.3.0
    4 : ./repoUpdate.sh[6]: svn: not found [No such file or directory]
    Done!
    BUILD SUCCESSFUL (total time: 2 seconds)
    

    Then I tried modifying the repoUpdate.sh file, adding the absolute path for the svn command (Thank you, @ymnk)

    exec 2>&1
    echo  ' ****Repository update****'
    echo  ' Location: /home/cissys/repo/'
    echo -e ' Command: svn update /home/cissys/repo/2.3.0'
    
    /opt/subversion/bin/svn update /home/cissys/repo/2.3.0
    

    For my java, I got just what I was looking for:

    run:
    1 :  ****Repository update****
    2 :  Location: /home/cissys/repo/
    3 :  Command: svn update /home/cissys/repo/2.3.0
    4 : At revision 9443.
    Done!
    BUILD SUCCESSFUL (total time: 3 seconds)
    

    I found out that the $PATH I get from the session through java is not the same that I get directly on the linux console. So adding the svn path actually did the trick. Thank you very much for your help!

提交回复
热议问题