Cross-platform way to open a file using Java 1.5

后端 未结 8 783

I\'m using Java 1.5 and I\'d like to launch the associated application to open the file. I know that Java 1.6 introduced the Desktop API, but I need a solution for J

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 18:45

    public static boolean isWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        return os.indexOf("windows") != -1 || os.indexOf("nt") != -1;
    }
    public static boolean isMac() {
        String os = System.getProperty("os.name").toLowerCase();
        return os.indexOf("mac") != -1;
    }
    public static boolean isLinux() {
        String os = System.getProperty("os.name").toLowerCase();
        return os.indexOf("linux") != -1;
    }
    public static boolean isWindows9X() {
        String os = System.getProperty("os.name").toLowerCase();
        return os.equals("windows 95") || os.equals("windows 98");
    }
    

    and

     if (isLinux())
      {
         cmds.add(String.format("gnome-open %s", fileName));
         String subCmd = (exec) ? "exec" : "openURL";
         cmds.add(String.format("kfmclient "+subCmd+" %s", fileName));
      }
      else if (isMac())
      {
         cmds.add(String.format("open %s", fileName));
      }
      else if (isWindows() && isWindows9X())
      {
         cmds.add(String.format("command.com /C start %s", fileName));
      }
      else if (isWindows())
      {
         cmds.add(String.format("cmd /c start %s", fileName));
      }
    

提交回复
热议问题