Java calls the browser to open the full instance of the web page
- 2020-04-01 03:53:07
- OfStack
This article illustrates how Java calls a browser to open a web page. Share with you for your reference. The specific implementation method is as follows:
package com.yifang.demo;
import java.io.File;
public class OpenPageDemo {
public static void main(String[] args) {
try {
//String url = "http://www.baidu.com";
String url = "//www.jb51.net/";
java.net.URI uri = java.net.URI.create(url);
//Gets the current system desktop extension
java.awt.Desktop dp = java.awt.Desktop.getDesktop();
//Determine whether the system desktop supports the functionality to be performed
if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
//File file = new File("D:\aa.txt");
//dp.edit(file);// Edit the file
dp.browse(uri);//Gets the system default browser open link
// dp.open(file);// Open the file by default
// dp.print(file);// Print the document with a printer
}
} catch (java.lang.NullPointerException e) {
//This throws an exception if the uri is null
e.printStackTrace();
} catch (java.io.IOException e) {
//The system default browser could not be obtained
e.printStackTrace();
}
}
}
I hope this article has been helpful to your Java programming.