I have a Linux command that I cannot execute from Java:
[ ! -d "/tmp/flow" ] && echo "The directory DOES NOT exist."
It works fine in command line but when I use the following code:
String command = "[ ! -d \"/tmp/flow\" ] && echo \"The directory DOES NOT exist.\"";
Process proc = Runtime.getRuntime().exec(command);
Boolean successful = proc.waitFor()==0 && proc.exitValue()==0;
System.out.println("successful:"+successful);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
System.out.println("************** input");
while ((line = stdInput.readLine()) != null) {
System.out.println(line);
}
System.out.println("************** error");
while ((line = stdError.readLine()) != null) {
System.out.println(line);
}
it says that
successful:false
************** input
************** error
[: missing ']'
Any idea? Is it possible that these IF bash operators cannot be called from Java...?
Thanks!
**************** UDATE ****************
vikhor@adnlt653-vm1:~> which '['
[ is a shell builtin
[ is /usr/bin/[
[ is /bin/[
Boolean successful = proc.waitFor()==0 && proc.exitValue()==0;there's no need to checkexitValue()if you've just checked the return ofwaitFor()as they will be the same. Print the actual int value instead of creating a Boolean object.