title: Source command not working through Java
body: <p>From last day, I have been trying to execute a command on Terminal (MAC) using JAVA but whatever I do nothing is working. </p>
  
  <p>I have the following 2 commands that I want to execute and get the output back in JAVA</p>
  
  <pre><code>source activate abc_env
  python example.py
  </code></pre>
  
  <p>Till now, I have tried the following methods without any output</p>
  
  <pre><code>String[] command = new String[] { "source activate abc_env", "python example.py"};
  String result = executeCommands(command);
  </code></pre>
  
  <p>Here is my executeCommands method</p>
  
  <pre><code>private static String executeCommands(String[] command) {
  
          StringBuffer output = new StringBuffer();
  
          Process p;
          try {
              for(int i=0; i&lt; command.length;i++)
              {
                  p = Runtime.getRuntime().exec(command[i]);
                  p.waitFor();
                  BufferedReader reader = 
                                  new BufferedReader(new InputStreamReader(p.getInputStream()));
  
                  String line = "";           
                  while ((line = reader.readLine())!= null) {
                      output.append(line + "\n");
                  }
                  System.out.println("Error output: " + p.exitValue());
                  System.out.println("Output:" + output);
  
              }
          } catch (Exception e) {
              e.printStackTrace();
              System.out.println("Here");
          }
          return output.toString();
      }
  </code></pre>
  
  <p>This gives me the following exception</p>
  
  <blockquote>
    <p>Cannot run program "source": error=2, No such file or directory</p>
  </blockquote>
  
  <p>I searched online and people say that source won't work like this and I should change the command to </p>
  
  <pre><code>String[] command = new String[] { "bash -c 'source activate abc_env'", "python example.py"};
  </code></pre>
  
  <p>Now, I donot get the exception but the command still does not work and it returns '2' as <strong>exitValue()</strong></p>
  
  <p>Then I tried to execute the commands as a script </p>
  
  <pre><code>#!/bin/bash
  source activate abc_env
  python example.py
  </code></pre>
  
  <p>I get the following exception when I read the .sh file as string and pass it to command</p>
  
  <blockquote>
    <p>Cannot run program "#!/bin/bash": error=2, No such file or directory</p>
  </blockquote>
  
  <p>So, my question is how to run the source command followed by python command properly through Java ? My final goal is execute some python from Java.</p>
  
  <p>EDIT1:
  If I try the following command and print the output stream</p>
  
  <pre><code>String[] command = {
                  "/bin/bash",
                  "-c",
                  "source activate cvxpy_env"
          };
  
  executeCommand(command));
  </code></pre>
  
  <p>Output Stream:</p>
  
  <blockquote>
    <p>ExitValue:1</p>
    
    <p>ErrorOutput:/bin/bash: activate: No such file or directory</p>
  </blockquote>
  
  <p>If I try the same command but with single quotes around 'source activate abc_env'. I get the following output</p>
  
  <blockquote>
    <p>ExitValue:127</p>
    
    <p>ErrorOutput:/bin/bash: source activate cvxpy_env: command not found</p>
  </blockquote>
  
  <p>Solution:</p>
  
  <pre><code>String[] command = {
                  "/bin/bash",
                  "-c",
                  "source /Users/pc_name/my_python_library/bin/activate abc_env;python example.py"
          };
  </code></pre>