Tim*_*per 146
java.awt.Desktop 是你正在寻找的课程.
import java.awt.Desktop;
import java.net.URI;
// ...
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("http://www.example.com"));
}
Run Code Online (Sandbox Code Playgroud)
Bra*_*mar 34
这是我的代码.它将在默认浏览器(跨平台解决方案)中打开给定URL.
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Browser {
public static void main(String[] args) {
String url = "http://www.google.com";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
krz*_*ste 31
对我来说,使用Desktop.isDesktopSupported()的解决方案不起作用(Windows 7和ubuntu).请尝试从java代码打开浏览器:
视窗:
Runtime rt = Runtime.getRuntime();
String url = "http://stackoverflow.com";
rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
Run Code Online (Sandbox Code Playgroud)
苹果电脑
Runtime rt = Runtime.getRuntime();
String url = "http://stackoverflow.com";
rt.exec("open " + url);
Run Code Online (Sandbox Code Playgroud)
Linux的:
Runtime rt = Runtime.getRuntime();
String url = "http://stackoverflow.com";
String[] browsers = { "epiphany", "firefox", "mozilla", "konqueror",
"netscape", "opera", "links", "lynx" };
StringBuffer cmd = new StringBuffer();
for (int i = 0; i < browsers.length; i++)
if(i == 0)
cmd.append(String.format( "%s \"%s\"", browsers[i], url);
else
cmd.append(String.format(" || %s \"%s\"", browsers[i], url);
// If the first didn't work, try the next browser and so on
rt.exec(new String[] { "sh", "-c", cmd.toString() });
Run Code Online (Sandbox Code Playgroud)
如果要使用多平台应用程序,则需要添加操作系统检查(例如):
String os = System.getProperty("os.name").toLowerCase();
Run Code Online (Sandbox Code Playgroud)
视窗:
os.indexOf("win") >= 0
Run Code Online (Sandbox Code Playgroud)
苹果电脑:
os.indexOf("mac") >= 0
Run Code Online (Sandbox Code Playgroud)
Linux的:
os.indexOf("nix") >=0 || os.indexOf("nux") >=0
Run Code Online (Sandbox Code Playgroud)
希望你不介意,但我从上面拼凑了所有有用的东西,并提出了一个完整的课程以供测试......
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class MultiBrowPop {
public static void main(String[] args) {
OUT("\nWelcome to Multi Brow Pop.\nThis aims to popup a browsers in multiple operating systems.\nGood luck!\n");
String url = "http://www.birdfolk.co.uk/cricmob";
OUT("We're going to this page: "+ url);
String myOS = System.getProperty("os.name").toLowerCase();
OUT("(Your operating system is: "+ myOS +")\n");
try {
if(Desktop.isDesktopSupported()) { // Probably Windows
OUT(" -- Going with Desktop.browse ...");
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI(url));
} else { // Definitely Non-windows
Runtime runtime = Runtime.getRuntime();
if(myOS.contains("mac")) { // Apples
OUT(" -- Going on Apple with 'open'...");
runtime.exec("open " + url);
}
else if(myOS.contains("nix") || myOS.contains("nux")) { // Linux flavours
OUT(" -- Going on Linux with 'xdg-open'...");
runtime.exec("xdg-open " + url);
}
else
OUT("I was unable/unwilling to launch a browser in your OS :( #SadFace");
}
OUT("\nThings have finished.\nI hope you're OK.");
}
catch(IOException | URISyntaxException eek) {
OUT("**Stuff wrongly: "+ eek.getMessage());
}
}
private static void OUT(String str) {
System.out.println(str);
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用Runtime创建跨平台解决方案:
import java.awt.Desktop;
import java.net.URI;
public class App {
public static void main(String[] args) throws Exception {
String url = "http://stackoverflow.com";
if (Desktop.isDesktopSupported()) {
// Windows
Desktop.getDesktop().browse(new URI(url));
} else {
// Ubuntu
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常简单,只需编写以下代码:
String s = "http://www.google.com";
Desktop desktop = Desktop.getDesktop();
desktop.browse(URI.create(s));
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想加载 URL,则只需将您的浏览器名称写入字符串值,例如,
String s = "chrome";
Desktop desktop = Desktop.getDesktop();
desktop.browse(URI.create(s));
Run Code Online (Sandbox Code Playgroud)
执行程序后,它会自动打开浏览器并显示空 URL
如Tim Cooper提供的答案中所述,java.awt.Desktop自Java版本6(1.6)以来已提供此功能,但有以下警告:
使用isDesktopSupported()方法确定Desktop API是否可用.在Solaris操作系统和Linux平台上,此API依赖于Gnome库.如果这些库不可用,则此方法将返回false.
对于不支持或提供的平台,请java.awt.Desktop查看BrowserLauncher2项目.它是由Eric Albert最初编写和发布的BrowserLauncher类派生并稍微更新的.我在一个多平台Java应用程序中成功使用了原始的BrowserLauncher类,该应用程序在21世纪初期通过Web浏览器界面在本地运行.
请注意,BrowserLauncher2是根据GNU宽通用公共许可证获得许可的.如果该许可证是不可接受的,请查找原始BrowserLauncher的副本,该副本具有非常自由的许可证:
本代码为Eric Albert(ejalbert@cs.stanford.edu)版权所有1999-2001,并且可以不受任何形式重新分发或修改,只要本段中评论的部分到评论末尾未被删除.作者要求他通知任何使用此代码的应用程序,applet或其他二进制文件,但这更多地出于好奇而不是任何需要.本软件不含保修.对于因使用此软件而导致的任何数据或功能损失或任何不利或意外影响,作者不予以回应.
致谢:Steven Spencer,JavaWorld杂志(Java提示66)感谢Ron B. Yeh,Eric Shapiro,Ben Engber,Paul Teitlebaum,Andrea Cantatore,Larry Barowski,Trevor Bedzek,Frank Miedrich和Ron Rabakukk
BrowserLauncher2以外的项目可能还更新了原始的BrowserLauncher,以便考虑自2001年以来浏览器和默认系统安全设置的变化.
| 归档时间: |
|
| 查看次数: |
131649 次 |
| 最近记录: |