Desktopは、ファイルの拡張子に応じて処理(開く・編集)を行う為のクラス(JDK1.6以降)。
開いたり編集したりするアプリケーションと拡張子がOSによって関連付けられていることが条件。
つまりWindows向けという感じ。UNIXだと使えるか?
(Windowsでも、関連付けられていない拡張子だったら例外が発生する)
import java.awt.Desktop; import java.io.File; import java.io.IOException;
public class Edit {
public static void main(String[] args) throws IOException {
File f = new File("C:/temp/sample.html");
Desktop desktop = Desktop.getDesktop();
desktop.open(f);
// desktop.edit(f);
}
}
OSで割り当てられているデフォルトのブラウザーを起動し、URLを開く。[2009-04-20]
public static void main(String[] args) throws URISyntaxException {
URI uri = new URI("http://www.google.co.jp");
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(uri);
} catch (IOException e) {
e.printStackTrace();
}
}