1

I am trying to run Unix find command from Java but it is giving me below error . Please suggest right approach on this -

java -cp automation.jar com.amex.scoring.dao.HiveDAO

/bin/sh: find /axp/rim/nemo/dev/logs -type f -mtime -5 -exec ls {} \;: No such file or directory

Java Code

private static final String COMMAND = "find /axp/rim/nemo/dev/logs  -type f -mtime -5 -exec ls {} \\;";
private static final String SHELL_NAME = "/bin/sh";

public static void main(String[] args) throws IOException {
    final ProcessBuilder pb = new ProcessBuilder(SHELL_NAME,COMMAND);
    pb.redirectErrorStream(true);
    final Process process = pb.start();
    final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}
3
  • @Jens , sorry did not get you . where do you want me to add full path ? Commented Aug 17, 2016 at 9:03
  • to the find command and i think also for the ls command Commented Aug 17, 2016 at 9:03
  • 1
    Unnecessary double escape: \\; Commented Aug 17, 2016 at 9:07

1 Answer 1

1

See it :

package com.raj.shell;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

    public static void main(String[] args) {

        ExecuteShellComand obj = new ExecuteShellComand();

        String output = obj.executeCommand();

        System.out.println(output);

    }

    private String executeCommand() {

        StringBuffer output = new StringBuffer();

        try {

            Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "cd && cat test.txt | grep Hello"});
            p.waitFor();
            BufferedReader reader
                    = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Consider adding some explanation; to point out why your input is working; and his is not. Just dropping code doesn't make a good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.