How can i open Microsoft Edge using Robot in Java?

import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RickRoll {
    public static void main(String[] args) throws AWTException, IOException, InterruptedException {
        
        //create Robot
        Robot robot = new Robot();

        //runtime to open Microsoft Edge
        Runtime runtime = Runtime.getRuntime();
        String command = "msedge.exe";

What is below is supposed to run msedge

    runtime.exec(command);
    try {
        Thread.sleep(2000);
    }
    catch (InterruptedException e){
        
        e.printStackTrace();
    }
    robot.delay(1000);
}

}

I dont understand why I keep getting an error instead of it running msedge. What am i doing wrong?


Solution 1:

You need to run it with cmd. So it would be:

//runtime to open Microsoft Edge
Runtime runtime = Runtime.getRuntime();
String command = "cmd.exe /C start microsoft-edge:http://www.google.com";
runtime.exec(command);