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

	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");
	}

 

posted @ 2022-10-04 00:15  IT情深  阅读(313)  评论(0编辑  收藏  举报