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?
CodePudding user response:
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);
CodePudding user response:
You have several options, including using class java.lang.Runtime, but I would say that is the least recommended.
As @CharlieArmstrong mentioned in his comment, you need to execute file msedge.exe. If the folder containing that file is not in the PATH environment variable, you need to supply the full path to the file. You can use class java.lang.ProcessBuilder to execute the file.
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
Path path = Paths.get("C:",
"Program Files (x86)",
"Microsoft",
"Edge",
"Application",
"msedge.exe");
ProcessBuilder pb = new ProcessBuilder(path.toString());
try {
pb.start();
}
catch (IOException x) {
x.printStackTrace();
}
}
}
The above code will open Edge at its home page. If you want to open a particular Web page, just add the URL.
public static void main(String[] args) {
Path path = Paths.get("C:",
"Program Files (x86)",
"Microsoft",
"Edge",
"Application",
"msedge.exe");
ProcessBuilder pb = new ProcessBuilder(path.toString(), "http://www.facebook.com");
try {
pb.start();
}
catch (IOException x) {
x.printStackTrace();
}
}
Alternatively, you can use class java.awt.Desktop. If you just want to launch Edge, use method open. Again, if Edge is not on your PATH then you need to supply the full path.
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DesktopT {
public static void main(String[] args) {
if (Desktop.isDesktopSupported()) {
Path path = Paths.get("C:",
"Program Files (x86)",
"Microsoft",
"Edge",
"Application",
"msedge.exe");
File f = path.toFile();
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(f);
}
catch (IOException x) {
x.printStackTrace();
}
}
}
}
If you want to open a particular Web page then use method browse.
(Note that method browse will use your default Web browser which may not necessarily be Edge.)
public static void main(String[] args) {
if (Desktop.isDesktopSupported()) {
try {
URI uri = new URI("https://www.yahoo.com");
Desktop desktop = Desktop.getDesktop();
desktop.browse(uri);
}
catch (URISyntaxException | IOException x) {
x.printStackTrace();
}
}
}
Note that all the above code was run with JDK 17.0.2 on Windows 10. When run, the Java code launched Edge and terminated. However, class Desktop also launches the Event Dispatch Thread (EDT) which may not terminate, if you are using an earlier JDK version. If that is the case then you can add the following as the last line in method main.
java.lang.System.exit(0);
This line will terminate the JVM.
