title: Getting awk return value with Java
body: <p><br>
  I am writing a simple shell command for our Java application. I have no experience in Linux shell and need a simple command. What i try to do is get user's active window's process name. From another question I found a script as follows:</p>
  
  <pre><code>ps -e | grep $(xdotool getwindowpid $(xdotool getwindowfocus)) | grep -v grep | awk '{print $4}'
  </code></pre>
  
  <p>Source:<a href="https://unix.stackexchange.com/questions/38867/is-it-possible-to-retrieve-the-active-window-process-title-in-gnome">https://unix.stackexchange.com/questions/38867/is-it-possible-to-retrieve-the-active-window-process-title-in-gnome</a></p>
  
  <p>I am able to test and print the result but. Can't get the value from Java side. However if I run a simple script like</p>
  
  <pre><code>echo asdasd
  </code></pre>
  
  <p>I can get the "asdasd" String just fine. As i understand Java can only get echo commands.</p>
  
  <p>At java side I use this method:
      private String executeCommand(String command) {</p>
  
  <pre><code>    StringBuffer output = new StringBuffer();
  
      Process p;
      try {
          p = Runtime.getRuntime().exec(command);
          p.waitFor();
          BufferedReader reader = 
                          new BufferedReader(new InputStreamReader(p.getInputStream()));
  
                      String line = "";           
          while ((line = reader.readLine())!= null) {
              output.append(line + "\n");
          }
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      return output.toString();
  
  }
  </code></pre>
  
  <p>Source: <a href="http://www.mkyong.com/java/how-to-execute-shell-command-from-java/" rel="nofollow noreferrer">http://www.mkyong.com/java/how-to-execute-shell-command-from-java/</a></p>
  
  <p>------SOLVED------<br></p>
  
  <p>Solved thanks to smasseman's answer, however I changed the command String with  th following String array,</p>
  
  <pre><code>String[] args = {"/bin/sh","-c",command };
  </code></pre>
  
  <p>"command" is the script itself not the filepath as it gave errors.</p>