简体   繁体   English

java 使用 shell 命令

[英]java using shell commands

So trying to use "cp" to copy a file from one location to another - the file is denominated with a file name containing white space ("test test").因此,尝试使用“cp”将文件从一个位置复制到另一个位置 - 该文件以包含空格的文件名命名(“test test”)。 When invoking command in the shell (bash) it works fine, but called from java it fails.在 shell (bash) 中调用命令时,它工作正常,但从 java 调用它失败。 I use escape characters.我使用转义字符。 Code:代码:

import java.io.*;

public class Test {

  private static String shellify(String path) {
    String ret = path;
    System.out.println("shellify got: " + ret);
    ret = ret.replace​(" ", "\\ ");
    System.out.println("shellify returns: " + ret);
    return ret;
  }    

  private static boolean copy(String source, String target) {
    String the_command = ""; // will be global later
    boolean ret = false;
    try {
      Runtime rt = Runtime.getRuntime();
      String source_sh = shellify(source);
      String target_sh = shellify(target);
      the_command = new String
    ("cp -vf " + source_sh + " " + target_sh);
      System.out.println("copy execing: " + the_command);
      Process p = rt.exec(the_command);
      InputStream is = p.getInputStream();
      BufferedReader br = new BufferedReader
    (new InputStreamReader(is));
      String reply = br.readLine();
      System.out.println("Outcome; " + reply);
      ret = (reply != null) && reply.contains("->");
    } catch(Exception e) {
      System.out.println(e.getMessage());
    }
    the_command = "";
    return ret;
  }

  public static void main(String[] args) {
    String source = "test1/test test";
    String target = "test2/test test";
    if(copy(source, target))
      System.out.println("Copy was successful");
    else
      System.out.println("Copy failed");
  }

}

...and the outcome is this ...结果是这样的

shellify got: test1/test test
shellify returns: test1/test\ test
shellify got: test2/test test
shellify returns: test2/test\ test
copy execing: cp -vf test1/test\ test test2/test\ test
Outcome; null
Copy failed

whereas if I use bash the copy succeeded (big surprise).而如果我使用 bash 复制成功(大惊喜)。

Sino-Logic-IV:bildbackup dr_xemacs$ cp -vf test1/test\ test test2/test\ test
test1/test test -> test2/test test

Can anyone please tell me why this is?谁能告诉我这是为什么? Copying files without white spaces works just fine.复制没有空格的文件就可以了。

/dr_xemacs /dr_xemacs

You should pass the arguments individually and they do not need escaping in shellify because the String[] version of exec provides each argument to the script separately as one argument even if they contain spaces:您应该单独传递 arguments 并且它们在 shellify 中不需要 escaping 因为 exec 的 String[] 版本将每个参数作为一个参数单独提供给脚本,即使它们包含空格:

String[]the_command = new String[] {"cp","-vf", source, target);

You may need to fix PATH available to Java VM if cp is not present in one of the PATH directories, or you could fully qualify the path to the executable cp in the_command [0] .如果 PATH 目录之一中不存在cp ,您可能需要修复 Java VM 可用的 PATH,或者您可以完全限定the_command [0]中可执行文件 cp 的路径。

Your code is missing a line to check the cp / process exit code, add waitFor after consuming the STDOUT / getInputStream() :您的代码缺少一行来检查cp / process 退出代码,在使用 STDOUT / getInputStream()后添加waitFor

int rc = p.waitFor();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM