如何从 Java Swing 应用程序在默认浏览器中打开 HTML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2323110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:08:24  来源:igfitidea点击:

How can I open an HTML file in the default browser from a Java Swing application?

javaswingbrowser

提问by kovike

My Java Swing application generates an HTML file, and I want to open it with the default browser when it is generated and saved. How can I do this?

我的 Java Swing 应用程序生成了一个 HTML 文件,我想在生成并保存它时使用默认浏览器打开它。我怎样才能做到这一点?

采纳答案by Chandra Sekar

If you are using Java 6, use Desktop.open(). It allows you to open any file with the default application associated with its file type on the system.

如果您使用的是 Java 6,请使用Desktop.open()。它允许您使用与其在系统上的文件类型相关联的默认应用程序打开任何文件。

回答by Nate

If you're not using Java 6 (or not sure your users will) - you can use Bare Bones Browser Launcherto launch the default browser. It uses Java 6 Desktop.open() if available, and falls back to platform specific approaches if it's not.

如果您没有使用 Java 6(或者不确定您的用户会使用) - 您可以使用Bare Bones Browser Launcher来启动默认浏览器。如果可用,它使用 Java 6 Desktop.open(),如果不是,则回退到特定于平台的方法。

回答by Salman

You can try this:

你可以试试这个:

Runtime.getRuntime().exec("hh.exe index.chm");

回答by Junny Lee

Check this page, this can give you a much more detailed usage of the Desktop API.

检查此页面,这可以为您提供桌面 API 的更详细用法。