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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jeff
  • 674
  • 1
  • 5
  • 17
  • 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--GoFundMonica May 23 '13 at 22:08

1 Answers1

1

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

String[] command ={"bash", "bash/testbash.sh"};
user207421
  • 305,947
  • 44
  • 307
  • 483