使用闭包编译器格式化 javascript 文件时得到 java.nio.file.InvalidPathException:Illegal char <:> at index 7

Got java.nio.file.InvalidPathException:Illegal char <:> at index 7 when formatting javascript file using closure compiler

我正在使用 google 闭包编译器来格式化我的 Web 项目中的一些 java 脚本文件。因此,我正在编写一个 Java 应用程序来遍历根目录下的所有 js 文件。出于测试目的,我只尝试了一个 java 脚本文件,如下所示:

public static void main(String[] args){
        JSBeautifier js = new JSBeautifier();
        js.beautify("'C:/CodeScanner/src/test/resources/testJS.js'");
    }

    public void beautify(String filePath) {
        String srcFileArg = "--js " + filePath;

        ProcessBuilder pb = new ProcessBuilder("java", "-jar" , "compiler.jar", srcFileArg).inheritIO();

        try {
            Process p  = pb.start();
            p.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while(br.readLine() != null){
                System.out.println(br.readLine());
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

然而,当它被执行时,它给出了异常:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 7: --js 'C:/CodeScanner/src/test/resources/testJS.js'
    at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
    at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
    at java.nio.file.Paths.get(Paths.java:84)
    at com.google.javascript.jscomp.CommandLineRunner.findJsFiles(CommandLineRunner.java:1753)
    at com.google.javascript.jscomp.CommandLineRunner.findJsFiles(CommandLineRunner.java:1730)
    at com.google.javascript.jscomp.CommandLineRunner$Flags.getJsFiles(CommandLineRunner.java:928)
    at com.google.javascript.jscomp.CommandLineRunner.initConfigFromFlags(CommandLineRunner.java:1325)
    at com.google.javascript.jscomp.CommandLineRunner.<init>(CommandLineRunner.java:1176)
    at com.google.javascript.jscomp.CommandLineRunner.main(CommandLineRunner.java:1834)

虽然在命令行中这个操作(java -jar compiler.jar --js 'C:/CodeScanner/src/test/resources/testJS.js')可以成功执行。

有人可以帮忙吗?

看起来 ProcessBuilder 正在尝试执行:

java "-jar" "compiler.jar" "--js 'C:/CodeScanner/src/test/resources/testJS.js'"

导致 js 编译器看到:

argv[0] = "--js 'C:/CodeScanner/src/test/resources/testJS.js'"

而不是:

argv[0] = "--js"
argv[1] = "'C:/CodeScanner/src/test/resources/testJS.js'"

要更正此问题,我会尝试更改:

String srcFileArg = "--js " + filePath;
ProcessBuilder pb = new ProcessBuilder("java", "-jar" , "compiler.jar", srcFileArg).inheritIO();

进入

String srcFileArg = filePath;
ProcessBuilder pb = new ProcessBuilder("java", "-jar" , "compiler.jar", "--js", srcFileArg).inheritIO();