资源下载执行命令行程序“查找”返回错误 [英] Java execute command line program 'find' returns error

查看:97
本文介绍了资源下载执行命令行程序“查找”返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从终端下面的工作没有问题。

 找到TESTDIR型​​的F -exec的md5sum {} \\;

其中, TESTDIR 是包含一些文件的目录(例如文件1,文件2和文件3)。

不过,我得到在Java中使用下面的错误

 运行RT =调用Runtime.getRuntime();
流程PR = rt.exec(找TESTDIR型​​的F -exec的md5sum {} \\\\;);

该错误是

 发现:缺少参数`-exec'

我相信我是正确的转义字符。我曾尝试几种不同的格式,我不能得到这个工作。

更新 @jtahlborn完美地回答了这个问题。但该命令已经略有改变计算的md5sum之前在dir每个文件进行排序,并如下(我已经接受了原来的问题,所以我就买了别人啤酒优秀的答案,如果他们能拿出格式为此,我已经想尽组合,我能想到遵循以下没有成功的答案。)


  

查找TESTDIR型​​的F -exec的md5sum {} + | awk的{打印$ 1} |排序|
  的md5sum;


新的更新

有关管道,你需要一个壳,所以我结束了这一点,伟大的工程,你仍然可以得到的输出。

 运行RT =调用Runtime.getRuntime();
流程PR = rt.exec(新的String []
{
    嘘,-l,-c,查找+ directory.getPath()+型的F -exec的md5sum {} + | awk的'{打印$ 1}'|排序|的md5sum
});


解决方案

使用的多参数调用exec(否则,您可以得到由逃脱的规则咬伤)。此外,由于你不是从一个shell脚本中调用,您不必逃跑的分号:

 过程PR = rt.exec(新的String [] {发现,TESTDIR,型,F,-exec,的md5sum {},;});

The following works from the terminal no problem

find testDir -type f -exec md5sum {} \;

Where testDir is a directory that contains some files (for example file1, file2 and file3).

However, I get an error using the following in Java

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("find testDir -type f -exec md5sum {} \\;");

The error is

find: missing argument to `-exec'

I believe I am escaping the characters correctly. I have tried several different formats and I cannot get this to work.

UPDATE @jtahlborn answered the question perfectly. But the command has now changed slightly to sort each file in the dir before calculating the md5sum and is as follows (I have already accepted the excellent answer for the original question so I'll buy somebody a beer if they can come up with the format for this. I have tried every combination I can think of following the answer below with no success.)

"find testDir -type f -exec md5sum {} + | awk {print $1} | sort | md5sum ;"

NEW UPDATE

For pipe, you need a shell so I ended up with this, which works great and you can still get the output.

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[] 
{
    "sh", "-l", "-c", "find " + directory.getPath() + " -type f -exec md5sum {} + | awk '{print $1}' | sort | md5sum"
});

解决方案

use the multi-argument call to exec (otherwise you can get bitten by escape rules). also, since you aren't calling from a shell script, you don't need to escape the semicolon:

Process pr = rt.exec(new String[]{"find", "testDir", "-type", "f", "-exec", "md5sum", "{}", ";"});

这篇关于资源下载执行命令行程序“查找”返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆