Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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"}; –  Jeff May 23 '13 at 21:47
1  
Post that as an answer, and accept it. Answering your own questions is allowed. –  michaelb958 May 23 '13 at 22:08

1 Answer 1

up vote 1 down vote accepted

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

String[] command ={"bash", "bash/testbash.sh"};
share|improve this answer
    
+1 but maybe you could add a little explanatory commentary –  Bohemian May 24 '13 at 1:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.