6

I am trying to run a script using Java and ProcessBuilder. When I try to run, I receive the following message: error=2, No such file or directory.

I dont know what I am doing wrong but here is my code (ps: I tried to execute just the script without arguments and the error is the same:

String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};
ProcessBuilder p = new ProcessBuilder(command);

    try {  

        // create a process builder to send a command and a argument
        Process p2 = p.start(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
        String line;

        log.info("Output of running " + command + " is: ");
        System.out.println("Output of running " + command + " is: ");
        while ((line = br.readLine()) != null) {
            log.info(line);
        }

    }  
1

3 Answers 3

6

Try replacing

String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};

with

String[] command = {"/teste/teste_back/script.sh", argument1, argument};

Refer ProcessBuilder for more information.

ProcessBuilder(String... command)

Constructs a process builder with the specified operating system program and arguments.

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

2 Comments

Actually the problem were the quotation on the final of my string and also with arguments.
@JaganathanNanthakumar Hi , in my shellscript there is user and pw as arguments. But how do I initialize them? when I put them into the String command the program does not know what "user" and "pw" is...? Thank you
5

You can define a method with ProcessBuilder.

public static Map execCommand(String... str) {
    Map<Integer, String> map = new HashMap<>();
    ProcessBuilder pb = new ProcessBuilder(str);
    pb.redirectErrorStream(true);
    Process process = null;
    try {
        process = pb.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

    BufferedReader reader = null;
    if (process != null) {
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    }

    String line;
    StringBuilder stringBuilder = new StringBuilder();
    try {
        if (reader != null) {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        if (process != null) {
            process.waitFor();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    if (process != null) {
        map.put(0, String.valueOf(process.exitValue()));
    }

    try {
        map.put(1, stringBuilder.toString());
    } catch (StringIndexOutOfBoundsException e) {
        if (stringBuilder.toString().length() == 0) {
            return map;
        }
    }
    return map;
}

You can call the function to execute shell command or script

String cmds = "ifconfig";
String[] callCmd = {"/bin/bash", "-c", cmds};
System.out.println("exit code:\n" + execCommand(callCmd).get(0).toString());
System.out.println();
System.out.println("command result:\n" + execCommand(callCmd).get(1).toString());

Comments

3

Unless your script.sh has a comma in its name, that is the mistake:

String[] command = {"/teste/teste_back/script.sh" , argument1, argument};

Comments

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.