title: How to use MongoExport command in a java program?
body: <p>I'm able to export an entire collection using <strong>Mongoexport</strong> command in the Mongo shell. </p>
  
  <p>But, I'm trying to write a java program which uses a Mongoexport command to export entire collection of MongoDB into a CSV file.</p>
  
  <h1>My code:</h1>
  
  <pre><code>public class MongoExportSample {
      public static void main(String[] args) {
  
           String db = "pack";
           String col = "col";
           String Host="localhost";
           String Port="27017";
           String fileName = "D:/user/sample.csv";
           String command = "mongoexport --host Host --port Port --db " + db + " --collection " + col + " --csv --out " + fileName + "";
  
           try {
               Process process=Runtime.getRuntime().exec(command);
               int waitFor = process.waitFor();
               System.out.println("waitFor:: "+waitFor);
               BufferedReader success=new BufferedReader(new InputStreamReader(process.getInputStream()));
               BufferedReader error=new BufferedReader(new InputStreamReader(process.getErrorStream()));
  
               String s="";
               while ((s = success.readLine()) != null) {
               System.out.println(s);
               }
  
               while ((s = error.readLine()) != null) {
               System.out.println("Std ERROR : " + s);
               }
               } catch (Exception e) {
               e.printStackTrace();
               }
      }
  }
  </code></pre>
  
  <p>I'm facing <strong>java.io.IOException: Cannot run program "mongoexport": CreateProcess error=2, The system cannot find the file specified.</strong></p>
  
  <p>Can anyone please help me out regarding the same ...</p>
  
  <p><a href="https://i.stack.imgur.com/OmlbX.png" rel="nofollow noreferrer">Please check the screenshot for STDERR here</a></p>