How to restart modem using command in java

Hi i want to restart my adsl modem using command line in java. but cmd stops when username is requested and needs to be entered. what is the problem?


public class ExecuteShellComand

    public static void main(String[] args) throws IOException

        String tel = "telnet 23";
        String user = "admin";
        String pass = "admin";
        String reboot = "reboot";
        String command = "cmd /c start cmd.exe /c"+tel+"/c"+user+"/c"+pass+"/c"+reboot;

        Process child = Runtime.getRuntime().exec(command);



when username must be entered


source to share

2 answers

You may also need space between each of your arguments. By typing a command, you will send the command to a prompt that looks like this:

cmd /c start cmd.exe /ctelnet 23/cadmin/cadmin/creboot


Add System.out.println command (command); after creating the string to see what is actually in the String.

Change the code as follows:

String command = "cmd /c start cmd.exe /c "+tel+" /c "+user+" /c "+pass+" /c "+reboot;




You need to send the username and password and the command reboot

to the standard input (stdin) of the telnet command. To do this, create a process and then:

// You /probably/ don't need "cmd /c" here ...
Process child = Runtime.getRuntime().exec(tel);

// Write to stdin of child, force flush after each line
// Yes, you need to get an OutputStream here ... it connected to stdin of the child
PrintWriter out = new PrintWriter(child.getOutputStream(), true);


Note that you may have to read stdout while sending these commands to prevent blocking the child process. To do this, create a background thread. See this post:



All Articles