English 中文(简体)
Java 执行命令行程序查找返回错误
原标题:Java execute command line program find returns error

从终端完成的下列工程

find testDir -type f -exec md5sum {} ;

此处 testDir 是包含某些文件的目录( 例如文件1、 文件2和文件3) 。

然而,在爪哇使用以下文字时,我发现一个错误:

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

错误是

find: missing argument to `-exec 

我认为我逃避角色是正确的,我尝试了几种不同的格式,我无法使这个运作。

“强势”UPDATE

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

<强 > NEW 更新

对于管道,你需要一个贝壳 所以我结束了这个, 工作很好,你仍然可以得到输出。

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"
});
最佳回答

使用多参数的调用来执行( 否则您可以通过逃生规则被咬到) 。 另外, 由于您不是从一个 shell 脚本中调用, 您不需要跳过分号 :

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

对于像管子之类的东西, 你需要一个贝壳, 你没有得到它与Runtime. exec。

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"
});




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签