Search code examples
javacmdtelnet

Starting a connected telnet cmd window in Java


I'm currently attempting to make an application that will connect to a telnet server on startup. Currently I am attempting code similar to:

String ss = null;
Process p = Runtime.getRuntime().exec("cmd /c telnet localhost 4445");
BufferedWriter writeer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writeer.write("telnet localhost 4445");
writeer.flush();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
while ((ss = stdInput.readLine()) != null) {
    System.out.println(ss);
}

However, this does not open a terminal, though I assume it runs the desired command. I would like a terminal to open, a command to be done, shown in the 2nd line, and then leave the terminal open for user use.

Is there a way to do this? I have been trying for a while now and no solutions I can find actually give the desired output.


Solution

  • Hi what about using start ,

     Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"ping localhost && telnet localhost 4445\"");
    

    Will open a new window. But you'll not have access to the process since it is diffrent process initiated by exec.Still you can execute multip commands.