title: Java processbuilder and using environment variables
body: <p>What I want to do is I want to run a process, however because this process itself relies on environment variables, directly calling it causes error within the process. For those who are wondering what this is, it's <code>rake</code> tool. For this reason I thought maybe it's better to use <code>bash</code> and using it through <code>bash</code> would eliminate the issue. However that doesn't seem to be the case.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public static void runPB(String directory) throws IOException {
          ProcessBuilder processBuilder = new ProcessBuilder(
                  "/bin/bash");
          processBuilder.directory(new File(directory));
          Process process = processBuilder.start();
          OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream());
          osw.write("rake routes");
          osw.close();
          printStream(process.getErrorStream());
          printStream(process.getInputStream());
      }
  
      public static void printStream(InputStream is) throws IOException {
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line;
          while ((line = br.readLine()) != null) {
              System.out.println(line);
          }
      }
  </code></pre>
  
  <p>I know it is environment related issue because the error that I am getting is described here <a href="https://stackoverflow.com/questions/19061774/cannot-load-such-file-bundler-setup-loaderror">cannot load such file -- bundler/setup (LoadError)</a></p>
  
  <p>Further I checked <code>processBuilder.environment()</code> returns less environment variables than entering <code>env</code>. I went ahead and changed the <code>osw.write()</code> line and tried <code>echo $GEM_HOME</code> there, which doesn't print anything and if I do this on my OSs bash then I get the path, I also tried other common things like <code>echo $SHELL</code> and it prints the shell location in both Java code and in bash.</p>
  
  <p>So my questions are:</p>
  
  <p>1) Why is my operating system's environment variables are different than the <code>ProcessBuilder.environment()</code> method?</p>
  
  <p>2) Does <code>Process</code> class consider using environment variables that were given out by <code>ProcessBuilder.environment()</code>? If so then how can we add the missing ones from the operating system's level?</p>