如何在Linux环境下,使用Java打开默认浏览器 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我不了解你的问题,但Applet中可以这样实现 URL url = new URL(applet.getDocumentBase(), "http://...."); applet.getAppletContext().showDocument(url,"Test" ); 楼主有点不明白你的意思。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); }}} java本来就是跨系统的,任何系统装个jvm都是一样的,不存在在那个系统环境下 这句话对,但要分情况而定lz试试看Runtime.getRuntime().exec("/usr/bin/firefox " + url); 方法有很多的: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(); } } Desktop 需要jdk1.6支持的,对于Runtime.getRuntime().exec(),安全性不是很好,不知道还有没有其他方法 能够解释一下这句 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler"+ url);它是如何找到windows下默认的浏览器的 public static void openURL(String url) { try { browse(url); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + e.getLocalizedMessage()); } } private static void browse(String url) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InterruptedException, InvocationTargetException, IOException, NoSuchMethodException { String osName = System.getProperty("os.name", ""); if (osName.startsWith("Mac OS")) { 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")) { 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) throw new NoSuchMethodException("Could not find web browser"); else Runtime.getRuntime().exec(new String[] { browser, url }); } } 楼上这段代码部署到linux服务器后,在windows下访问,怎么没有反应 Java数组是不是类?在哪里可以查到这个类的成员和方法?它们和Array类的区别? 关于泛型在static函数中 AWT中在打开一个窗体后如何关闭前一个窗体? 我这程序是咋回事??竟然无中生有?? Java Swing 关于菜单过长的问题 不好意思 如何读取pkcs7文件(.p7b)? java中的synchronized的用法问题 怎样通过代码选中JTable中的一行? 新手问题。怎样往JTable中插入数据。 将只包含字母的单词放到String数组中,将数字放到int数组中。 刚发布了一个java多功能记事本程序,来这里推广一下....
URL url = new URL(applet.getDocumentBase(), "http://....");
applet.getAppletContext().showDocument(url,"Test" );
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);
}
}
}
lz试试看
Runtime.getRuntime().exec("/usr/bin/firefox " + url);
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();
}
}
public static void openURL(String url) {
try {
browse(url);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + e.getLocalizedMessage());
}
} private static void browse(String url) throws ClassNotFoundException, IllegalAccessException,
IllegalArgumentException, InterruptedException, InvocationTargetException, IOException,
NoSuchMethodException {
String osName = System.getProperty("os.name", "");
if (osName.startsWith("Mac OS")) {
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")) {
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)
throw new NoSuchMethodException("Could not find web browser");
else
Runtime.getRuntime().exec(new String[] { browser, url });
}
}