All*_*nde 2 java windows process
我一直在使用以下代码在我的Windows机器上使用Java打开Office文档,PDF等,并且它工作正常,除了某些原因,当文件名将其嵌入其中的多个连续空格如"File [SPACE] [ SPACE] Test.doc的".
我怎样才能做到这一点?我并不反对将整段代码编成罐头......但我宁愿不用一个调用JNI的第三方库替换它.
public static void openDocument(String path) throws IOException {
// Make forward slashes backslashes (for windows)
// Double quote any path segments with spaces in them
path = path.replace("/", "\\").replaceAll(
"\\\\([^\\\\\\\\\"]* [^\\\\\\\\\"]*)", "\\\\\\\"$1\"");
String command = "C:\\Windows\\System32\\cmd.exe /c start " + path + "";
Runtime.getRuntime().exec(command);
}
Run Code Online (Sandbox Code Playgroud)
编辑:当我用错误的文件窗口运行它抱怨找到文件.但是......当我直接从命令行运行命令行时,它运行得很好.