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

时间:2022-05-24 07:38:19

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文件,我想在生成和保存时使用默认浏览器打开它。我怎样才能做到这一点?

4 个解决方案

#1


19  

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()。它允许您使用与其文件类型相关联的默认应用程序打开任何文件。

#2


6  

If you're not using Java 6 (or not sure your users will) - you can use Bare Bones Browser Launcher to 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(),如果不可用,则使用特定于平台的方法。

#3


2  

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

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

#4


0  

You can try this:

你可以试试这个:

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

#1


19  

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()。它允许您使用与其文件类型相关联的默认应用程序打开任何文件。

#2


6  

If you're not using Java 6 (or not sure your users will) - you can use Bare Bones Browser Launcher to 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(),如果不可用,则使用特定于平台的方法。

#3


2  

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

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

#4


0  

You can try this:

你可以试试这个:

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