title: How to run NPM Command in Java using Process Builder
body: <pre><code>import java.io.BufferedOutputStream;
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.*;
  
  public class TestUnZip {
      public static void main(String[] args) throws IOException, InterruptedException{
          String destFolder="E:\\TestScript";
          /*
          *  Location where the Nodejs Project is Present
          */
          System.out.println(destFolder);
  
          String cmdPrompt="cmd";
          String path="/c";
          String npmUpdate="npm update";
          String npm="npm";
          String update="update";
  
          File jsFile=new File(destFolder);
          List&lt;String&gt; updateCommand=new ArrayList&lt;String&gt;();
          updateCommand.add(cmdPrompt);
          updateCommand.add(path);
          updateCommand.add(npmUpdate);
  
          runExecution(updateCommand,jsFile);
  
      }
      public static void runExecution(List&lt;String&gt; command, File navigatePath) throws IOException, InterruptedException{
  
          System.out.println(command);
  
          ProcessBuilder executeProcess=new ProcessBuilder(command);
          executeProcess.directory(navigatePath);
          Process resultExecution=executeProcess.start();
  
          BufferedReader br=new BufferedReader(new InputStreamReader(resultExecution.getInputStream()));
          StringBuffer sb=new StringBuffer();
  
          String line;
          while((line=br.readLine())!=null){
              sb.append(line+System.getProperty("line.separator"));
          }
          br.close();
          int resultStatust=resultExecution.waitFor();
          System.out.println("Result of Execution"+(resultStatust==0?"\tSuccess":"\tFailure"));
      }
  }
  </code></pre>
  
  <p>The Above Program works fine, but this program is depend on Windows Machine, I want to run the same program in other Machine as well.</p>
  
  <p>1) NPM is a Command comes as a bundle of <code>NodeJS</code>. (I run NodeJS as a service, I have defined the Environment Variable, so I can run npm update command from any folder)</p>
  
  <p>2) I can't find a work around to run the npm update command without using the <code>"cmd", "/c"</code>. If I do I get following error</p>
  
  <blockquote>
    <p>Exception in thread "main" java.io.IOException: Cannot run program "npm update" (in directory "E:\TestScript"): CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(Unknown Source)</p>
  </blockquote>
  
  <p>3) Do we have option of Running the npm update command as a parameter of <code>Node.exe</code>. If so can anyone provide me the proper work around. </p>
  
  <p>4) Same as I like, I use mocha framework to run the test script and result generates the .xml file. </p>
  
  <p>5) I want mocha command also being invoked using process builder.</p>