0

Can anyone help me get this code working in linux

    public static void main(String[] args) throws Exception,{   
    String s ="find . -exec ls -al {} \\;";
    Process exec = Runtime
            .getRuntime()
            .exec(s);
    exec.waitFor();
System.out.println(s);
    if (exec.getErrorStream().available() != 0) {
        BufferedInputStream bf = new BufferedInputStream(
                exec.getErrorStream());
        DataInputStream din = new DataInputStream(bf);
        System.out.println(din.readLine());
    }

    System.out.println(exec.exitValue());
}

I am getting the following output.

find . -exec ls -al {} \;
find: missing argument to `-exec'
1
Zombo
  • 1
  • 62
  • 391
  • 407
  • You are printing from the error stream. You probably want the process's standard output. –  Apr 16 '13 at 20:10
  • Please have a look at http://stackoverflow.com/questions/14441652/get-process-output-without-blocking – Alex Turbin Apr 16 '13 at 20:30
  • Currently I have an exit code of 1. I want exit code of 0(Success) . I am printing the error stream because I have exit code of 1(fail). Any of you guys got this code work with exit code of 0, let me know. – user2288031 Apr 16 '13 at 23:47

2 Answers2

1

Try using this:

String[] params = {"find", ".", "-exec", "ls", "-al", "{}", ";"};

instead of your plain String command.

Be careful though: The output is heavy (I tested with /dev/.) and the waitFor method may cause your I/O Buffer to burst.

Nicolas
  • 351
  • 1
  • 4
  • To elaborate on this answer: Calling `exec(String)` is unwise. It is far more reliable to call `exec(String[])`, which needs no backslash-escapes or quoting of arguments. – VGR Apr 17 '13 at 11:00
0

Too many backslash

find . -exec ls -al {} ';'
Zombo
  • 1
  • 62
  • 391
  • 407