Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 7165  / 3 Years ago, mon, october 18, 2021, 1:53:39

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, if I run this from a bash script or from Java using something like



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


I get the following error




find: missing argument to `-exec'




Any ideas?



UPDATE: This was answered correctly over on stackoverflow. I will close the question here. https://stackoverflow.com/questions/10704889/java-execute-command-line-program-find-returns-error


More From » bash

 Answers
5

The in -exec md6sum {} ; is necessary to prevent the shell from interpreting the ; character as command separator. If Java does not execute the command in a shell, try removing the escaping so that the code becomes:



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


I have just confirmed this behavior with the next test program:



import java.io.*;
class Xx {
public static void main(String args[]) throws Exception {
Process p = Runtime.getRuntime().exec("/bin/echo ;");
InputStream in = p.getInputStream();
int c;
while ((c=in.read()) != -1)
System.out.write((char)c);
p.waitFor();
}
}


Compiled with javac Xx.java, java Xx outputs ;. If I remove the , it'll print ; as expected.


[#38200] Tuesday, October 19, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fishutt

Total Points: 391
Total Questions: 137
Total Answers: 106

Location: Mexico
Member since Tue, Aug 11, 2020
4 Years ago
;