java:无法打开路径中有空格的finder窗口

时间:2017-05-04 13:09:43

标签: java macos javafx

我想在点击按钮时打开finder窗口并突出显示javafx中的特定文件,但是查找器窗口没有打开包含其名称空间的文件夹。 剪断的代码是: -

@FXML
    public void openFolder(ActionEvent event) {
        try {
            String upperCaseOperatingSystem=MCOBillingAppUtils.getOSName();
            String path=lblPath.getText();
            if(upperCaseOperatingSystem.contains("WINDOW")){
                Runtime.getRuntime().exec("explorer.exe /select,"+path);
            }else if(upperCaseOperatingSystem.contains("MAC")){
                Runtime.getRuntime().exec("open -R "+path);
            }
        } catch (IOException e) {
            e.printStackTrace();
            DialogMessageClass.infoBox("Something went Wrong. Please try again sometimes.", "Error");
        }catch(Exception e){
            e.printStackTrace();
            DialogMessageClass.infoBox("Something went Wrong. Please try again sometimes.", "Error");
        }
    }

路径是: / ME /开发文件夹/ Short_Closed / abc.pdf

尝试的路径值是: - / ME /开发文件夹/ Short_Closed / abc.pdf

/ ME / Development \ Folder / Short_Closed / abc.pdf(用空格反斜杠)

1 个答案:

答案 0 :(得分:2)

对于Mac(也可能是Windows),请使用较长形式的exec

// note no leading forward slash
String file = "ME/Development Folder/Short_Closed/abc.pdf"
File workingDir = new File("/");
String[] cmd = new String[]{"open", "-R", file};

Runtime.getRuntime().exec(cmd, null, workingDir);

您可以以任何方式打开要打开的工作目录和文件。 E.g。

// Note leading forward slash:
File file = new File("/ME/Development Folder/Short_Closed/abc.pdf") ;
File workingDir = file.getParentFile();
String filename = file.getName();

String[] cmd = new String[] {"open", "-R", filename} ;
Runtime.getRuntime().exec(cmd, null, workingDir);

也可以使用