• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: Desktop
title: Desktopでブラウザを起動
tags: [Desktop, Html]
author: aterai
pubdate: 2007-02-19T16:02:04+09:00
description: JDK 6で導入されたDesktopを使ってブラウザを起動します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTKu9HhvrI/AAAAAAAAAWc/iMheiQnF4hQ/s800/Desktop.png
---
* 概要 [#summary]
`JDK 6`で導入された`Desktop`を使ってブラウザを起動します。

#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTKu9HhvrI/AAAAAAAAAWc/iMheiQnF4hQ/s800/Desktop.png)

* サンプルコード [#sourcecode]
#code(link){{
if (!Desktop.isDesktopSupported()) {
  return;
if (Desktop.isDesktopSupported()) {
  try {
    Desktop.getDesktop().browse(new URI(href));
  } catch (IOException | URISyntaxException ex) {
    ex.printStackTrace();
  }
}
try {
  Desktop.getDesktop().browse(new URI(href));
} catch (IOException | URISyntaxException ex) {
  ex.printStackTrace();
}
}}

* 解説 [#explanation]
`Desktop`を使用することで、ブラウザのほかにも、メーラーや拡張子に関連付けられたデフォルトのアプリケーションを起動することが出来るようになります。
- `Desktop`を使用することで拡張子に関連付けられたデフォルトのアプリケーションを起動可能
- `OS`毎に対応するアプリケーションを`Runtime.getRuntime().exec(String[])`で実行せずにブラウザなどを起動可能

このため、`OS`毎に`Runtime.getRuntime().exec(String[])`でアプリケーションを切り替えなくても、ブラウザを起動することができます。

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Desktop.html Desktop (Java Platform SE 8)]
//- [http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/ Using the Desktop API in Java SE 6]
- [http://www.oracle.com/technetwork/articles/javase/index-135182.html Using the Desktop API in Java SE 6]
- [[Browserを起動>Swing/BrowserLauncher]]

* コメント [#comment]
#comment
#comment