-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
37 lines (33 loc) · 1.88 KB
/
Copy pathMain.java
File metadata and controls
37 lines (33 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.awt.Desktop; //allows applications to launch the default web browser, mail client, or associated application.
import java.io.File; //Imports the File class, used to interact with files and directories in the file system.
import java.io.IOException; //Imports IOException, which handles input/output exceptions, such as when the file path is incorrect or an application isn’t found.
public class Main {
public static void main(String[] args) {
String filePath = new File("home.html").getAbsolutePath();
if (!openWithDefaultBrowser(filePath)) {
if (!openWithSpecificBrowser("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", filePath)) { //Attempts to open home.html in the default browser using openWithDefaultBrowser. If unsuccessful (false), the code tries specific browsers.
if (!openWithSpecificBrowser("C:\\Program Files\\Mozilla Firefox\\firefox.exe", filePath)) {
openWithSpecificBrowser("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe", filePath);
}
}
}
}
private static boolean openWithDefaultBrowser(String filePath) {
try {
Desktop.getDesktop().browse(new File(filePath).toURI()); //Uses the Desktop class to open the file’s URI (home.html) in the default browser.
return true;
}
catch (IOException e) { //Catches any IOException
return false;
}
}
private static boolean openWithSpecificBrowser(String browserPath, String filePath) { //Uses the specified browser (given by browserPath) to open home.html.
try {
Process process = Runtime.getRuntime().exec(new String[]{browserPath, filePath});
process.getErrorStream().close();
return true;
} catch (IOException e) {
return false;
}
}
}