zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java调用系统默认浏览器打开URL 及执行CMD命令

JAVA浏览器系统执行命令 调用 打开 默认
2023-09-11 14:15:13 时间
	public static void runBroswer(String url) {
		Desktop desktop = Desktop.getDesktop();
		if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
			URI uri = null;
			try {
				uri = new URI(url);
			} catch (URISyntaxException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			try {
				desktop.browse(uri); // 使用系统默认的浏览器执行这个url
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// Runtime.getRuntime().exec("taskkill /F /IM Iexplore.exe");
			try {
				Runtime.getRuntime().exec("taskkill  /IM 360se.exe"); // 因为我系统默认的是360,然后关闭浏览器
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}



	public static void main(String[] args) {
		// TODO Auto-generated method stub
		runBroswer("https://www.baidu.com");
	}