使用java process builder复制文件的bash命令
Iam使用以下java代码执行bash命令,当尝试复制文件时,函数返回非零值,但Iam能够创建文件 复制命令:使用java process builder复制文件的bash命令,java,bash,processbuilder,file-copying,Java,Bash,Processbuilder,File Copying,Iam使用以下java代码执行bash命令,当尝试复制文件时,函数返回非零值,但Iam能够创建文件 复制命令:cp demo.txt/shared 输出为:127 创建文件命令:echo'sample text>demo.txt 输出为:0 public static int run(final String command) { String[] finalCommand; Process process=null; int temp=0; fin
cp demo.txt/shared
输出为:127
创建文件命令:echo'sample text>demo.txt
输出为:0
public static int run(final String command)
{
String[] finalCommand;
Process process=null;
int temp=0;
finalCommand = new String[3];
finalCommand[0] = "bash";//"/usr/bin/ksh";
finalCommand[1] = "-c";
finalCommand[2] = command;
try {
final ProcessBuilder processBuilder = new ProcessBuilder(finalCommand);
processBuilder.redirectErrorStream(true);
process = processBuilder.start();
temp=process.waitFor();
}
catch (IOException e)
{
System.out.println( e.getMessage());
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
return temp;
}
请帮助cp失败和回显工作的一种可能性是,cp是一个外部命令,而回显是一个内置命令 如果设置并导出了PATH环境变量,则只能通过其文件名找到外部命令 但在这种情况下,永远不要依赖路径-使用完整的路径名:
String command = "/bin/cp demo.txt /shared";
另外:您在/shared中有写权限吗?您有文件夹/shared吗?您有/shared的写权限吗?打印stdout,看看您在shell中尝试
cp demo.txt/shared
时遇到了什么错误。是否有效?