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

java:无法打开路径中有空间的finder窗口,java,macos,javafx,Java,Macos,Javafx,我想点击一个按钮打开finder窗口,并突出显示javafx中的一个特定文件,但finder窗口没有打开名称中包含空格的文件夹。 为同一目标截取的代码为:- @FXML public void openFolder(ActionEvent event) { try { String upperCaseOperatingSystem=MCOBillingAppUtils.getOSName(); String path=lbl

我想点击一个按钮打开finder窗口,并突出显示javafx中的一个特定文件,但finder窗口没有打开名称中包含空格的文件夹。 为同一目标截取的代码为:-

@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/Development Folder/Short_Closed/abc.pdf

尝试的路径值为:- /ME/Development Folder/Short_Closed/abc.pdf

/ME/Development\Folder/Short\u Closed/abc.pdf(用空格加反斜杠)

对于Mac(可能还有windows),请使用以下较长形式:

您可以按需要的任何方式分解要打开的工作目录和文件。例如

// 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);
也适用于Mac(可能也适用于windows),请使用较长形式的:

您可以按需要的任何方式分解要打开的工作目录和文件。例如

// 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);

也将起作用

尝试将路径用引号括起来,即
Runtime.getRuntime().exec(“open-R\”“+path+”\)
@James\u D尝试过,但没有成功。您尝试过从终端运行完全相同的东西吗?@James\u D from terminal正在打开。我的apporach在使用空格加反斜杠后在terminal上也可以正常工作。打印出传递给
exec
的实际字符串,看看与在terminal中使用的命令有什么不同。请尝试将路径用引号括起来,即
Runtime.getRuntime().exec(“open-R\”+path+“\”)
@James\u D试过了,但没有成功。你试过在终端上运行完全相同的东西吗?@James\u D,终端正在打开。My apporach在终端上用空格加反斜杠后也可以正常工作。打印出传递给
exec
的实际字符串,看看与终端上的命令有什么不同。