link_stat 来自 java 程序的错误 运行 "rsync"

link_stat error running "rsync" from java program

我想从 java 程序中 运行 rsync。这是代码:

        String cmd = "rsync -auv /rsrch1/rists/moonshot/data/foundation/FoundationMedicine/*.csv " + archive.getAbsolutePath();
        System.out.println(cmd);

        // capture stdout and stderr from running bash script
        Process p = Runtime.getRuntime().exec(cmd);
        String line;
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((line = err.readLine()) != null) {
            System.out.println(line);
        }
        p.waitFor();
        in.close();
        err.close();

然而,当我 运行 这个 java 程序时,我得到了错误:

rsync -auv /rsrch1/rists/moonshot/data/foundation/FoundationMedicine/*.csv /rsrch1/rists/moonshot/data/dev/foundation/validation/archive
sending incremental file list

sent 18 bytes  received 12 bytes  60.00 bytes/sec
total size is 0  speedup is 0.00
rsync: link_stat "/rsrch1/rists/moonshot/data/foundation/FoundationMedicine/*.csv" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

当我 运行 那个确切的命令 rsync -auv /rsrch1/rists/moonshot/data/foundation/FoundationMedicine/*.csv /rsrch1/rists/moonshot/data/dev/foundation/validation/archive 直接在服务器上时,它工作得很好。

我不明白为什么它不适用于 Java。

经过一番研究,我发现这是由命令中的“*”引起的。它是一个只有 bash 可以识别的通配符,我必须用 bash 运行 这个命令。使其适用于以下内容:

String[] cmd = new String[]{"/bin/bash", "rsync -auv /rsrch1/rists/moonshot/data/foundation/FoundationMedicine/*.csv " + archive.getAbsolutePath()};