Skip to main content
3 of 3
tone down.
c32hedge
  • 2.7k
  • 19
  • 39

Run unix commands using Jsch maven

Execute Unix commands using Jsch maven

I'm trying use this code for performing the following steps, but it's not working.

  1. String command1 = "ls -ltr"
  2. String command2 = "cd /test/user/"
  3. String command3 = "./test.sh"

Reference Link.

java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session=jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");
            
            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command1);
// From above command does work! It lists the files.
((ChannelExec)channel).setCommand(command2);

But the last line doesn't work--nothing happens.