Open file with default program using java 1.4

孤人 提交于 2019-12-24 15:06:06

问题


The Desktop.getDestop().open(File) launches the associated aplication to open the file.

The Desktop class is available since Java 1.6 - http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html

How to do the same using the 1.4 Java version?


回答1:


you can use the following to open files with the default application:

    /* build up command and launch */
    String command = "";
    String file = "FILE IN HERE";
    if (isLinux()) {
        command = "xdg-open " + file;
    } else if (isWindows()) {
        command = "cmd /C start " + file;
    } else
        return;

    try {
        Runtime.getRuntime().exec(command);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

available since 1.0: Runtime.




回答2:


Runtime.exec()

More details can be found at: http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html



来源:https://stackoverflow.com/questions/11106424/open-file-with-default-program-using-java-1-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!