Swing/Desktop のバックアップの現在との差分(No.12)
- category: swing folder: Desktop title: Desktopでブラウザを起動 tags: [Desktop, Html] author: aterai pubdate: 2007-02-19T16:02:04+09:00 description: JDK 6で導入されたDesktopを使ってブラウザを起動します。 image:
概要
JDK 6
で導入されたDesktop
を使ってブラウザを起動します。
Screenshot
Advertisement
サンプルコード
#spandel
if (!Desktop.isDesktopSupported()) {
#spanend
return;
#spanadd
if (Desktop.isDesktopSupported()) {
#spanend
try {
Desktop.getDesktop().browse(new URI(href));
} catch (IOException | URISyntaxException ex) {
ex.printStackTrace();
}
}
#spandel
try {
#spanend
Desktop.getDesktop().browse(new URI(href));
#spandel
} catch (IOException | URISyntaxException ex) {
#spanend
ex.printStackTrace();
#spandel
}
#spanend
View in GitHub: Java, Kotlin解説
Desktop
を使用することで、ブラウザのほかにも、メーラーや拡張子に関連付けられたデフォルトのアプリケーションを起動可能です。
-
Desktop
を使用することで拡張子に関連付けられたデフォルトのアプリケーションを起動可能 -
OS
毎に対応するアプリケーションをRuntime.getRuntime().exec(String[])
で実行せずにブラウザなどを起動可能
OS
毎にRuntime.getRuntime().exec(String[])
でアプリケーションを切り替えなくても、ブラウザを起動できます。