日期:2014-05-20 浏览次数:21153 次
import java.lang.reflect.Method;
import javax.swing.JOptionPane;
/**
* java打开浏览器
* @author wzf
* 2008-7-23 上午09:16:23
*/
public class URLOpener
{
/**
* test
* @param args
*/
public static void main(String args[])
{
openURL("");
}
/**
*
* @param url
*/
public static void openURL(String url)
{
String osName = System.getProperty("os.name");
try {
if(osName.startsWith("Mac OS"))
{
//doc
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] {String.class});
openURL.invoke(null, new Object[] {url});
}
else if(osName.startsWith("Windows"))
{
//Windows
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
}
else
{
//assume Unix or Linux
String[] browsers = {
"firefox", "opera", "konqueror",
"epiphany", "mozilla", "netscape"};
String browser = null;
for(int count = 0; count < browsers.length && browser == null;count++)
{
if(Runtime.getRuntime().exec(
new String[] {"which", browsers[count]}).waitFor() == 0)
{
browser = browsers[count];
}
}
if(browser != null)
{
Runtime.getRuntime().exec(new String[] {browser, url});
}
}
}
catch(Exception ex)
{
ExpWork.doExp(ex);
}
}
}
------解决方案--------------------
public static void main(String[] a) {
try {
URI uri = new URI("http://www.baidu.com");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null)
desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
------解决方案--------------------
路过 顶一下
------解决方案--------------------
帮顶,貌似最常见的是Runtime和DeskTop这两种了,DestTop需要jdk1.6支持,不知道在java中使用脚本行不行,等高手
------解决方案--------------------
学习了~~~