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 @   IT情深  阅读(324)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示