0

I am trying to execute a bash script that gets passed 4 arguments from java. I can execute the script without the arguments perfectly using this code:

    try 
        {       
            String command = "bash bash/testbash.sh";
            Runtime run = Runtime.getRuntime();
            Process pr = run.exec(command);
            pr.waitFor();
            BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line = "";
            while ((line=buf.readLine())!=null) 
            {
                System.out.println(line);
            }
         }  
        catch (Exception e) 
        {  
            System.out.println("Exception " + e);
            e.printStackTrace();
        }  
    }

So I was going to make a String[] to pass both the commands and the arguments to Runtime.exec(), like so:

    try 
        {       
            String[] command ={"bash bash/testbash.sh"};
            Runtime run = Runtime.getRuntime();
            Process pr = run.exec(command);
            pr.waitFor();
            BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line = "";
            while ((line=buf.readLine())!=null) 
            {
                System.out.println(line);
            }
         }  
        catch (Exception e) 
        {  
            System.out.println("Exception " + e);
            e.printStackTrace();
        }  
    }

which gives me this error:

Exception java.io.IOException: Cannot run program "bash bash/testbash.sh": error=2, No such file or directory
java.io.IOException: Cannot run program "bash bash/testbash.sh": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:483)
    at bashExecuter.main(bashExecuter.java:43)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
    ... 3 more

this makes no sense to me. The bash file clearly exists because I just used it with the String command, but when I use the same thing with String[] command its not there? How would I pass the command with arguments?

2
  • Figured it out. All spaces that would be in the command line now become different cells of String[]. So String command="bash bash/testbash.sh" becomes String[] command ={"bash", "bash/testbash.sh"}; Commented May 23, 2013 at 21:47
  • 1
    Post that as an answer, and accept it. Answering your own questions is allowed. Commented May 23, 2013 at 22:08

1 Answer 1

1

String[] command ={"bash bash/testbash.sh"};

String[] command ={"bash", "bash/testbash.sh"};
Sign up to request clarification or add additional context in comments.

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.