I am executing below code to establish a ssh connection to linux vm and execute commands using jsch from eclipse. code is like below:
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession("prkotagi", "slc10gst.us.oracle.com", 22);
session.setPassword("P@rs@1234nth");
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
But the issue here i am facing is when i execute the command in linux vm using eclispe. In eclipse the programm is running indefinitely like 30 mins. But actually the command should take 1 to 2 mins and it is showing log in my console. but the execution is getting strucked.
How to quit the command execution as soon as we got the o/p and the new line with bash $ displayed. But eclipse is not ending the execution.