Reputation: 986
I am connecting to SSH in Java with JSch jar. And need to show result in cmd not in IDE console view. Any guidance for this, it is possible to open a command prompt with the result?
Why we need result in cmd
means we need to take screenshot of the Command prompt window and send to client.
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Putty1 {
public static String txt;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JSchException, FileNotFoundException {
// TODO code application logic here
String host="HostName";
String user="xxxx";
String password="xxxx";
String command1="ls -ltr";
try{
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);
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;
txt = new String(tmp, 0, i);
System.out.print(txt);
}
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();
}
//System.out.println(txt);
try (PrintWriter out = new PrintWriter("C:\\Documents and Settings\\arahman9\\My Documents\\Downloads\\jpp\\prs72760.txt"))
{
out.println(txt);
}
}
}
Output:
Connected
total 12
drwxrwxrwt 2 arahman9 arahman9 4096 Jul 10 14:30 tmp
drwxr-xr-x 2 arahman9 arahman9 4096 Jul 10 14:30 public_html
drwxr-xr-x 2 arahman9 arahman9 4096 Jul 10 14:30 Documents
exit-status: 0
DONE
BUILD SUCCESSFUL (total time: 2 seconds)
Upvotes: 0
Views: 335
Reputation: 202474
When you execute a console application in IDE (Eclipse/Netbeans/etc), IDE redirects console application output to Console view.
But if you run your application in cmd.exe
it will use cmd.exe
console.
And if you run your application directly (via java.exe
), e.g. from Windows Run box, it will create its own console window.
That's just how console application behave. It has nothing to do with your code.
Also, here's some trick that can be used to make Eclipse not redirect console application output to Console view: https://www.reddit.com/r/eclipse/comments/1bk7a1/how_do_i_run_programs_in_a_separate_console_window/
Upvotes: 2