title: Run .exe file in Java from file location
body: <p>I have to open a .exe file from my Java program. So I tried following code First. </p>
  
  <pre><code>Process process = runtime.exec("c:\\program files\\test\\test.exe");
  </code></pre>
  
  <p>But I was getting some error. Then I found out that the exe has to be launched from that location that is c://program files/test/ only then it will open with out errors.  So I decided to write a .bat file and execute so that it will cd to that location and execute the .exe file.  </p>
  
  <p>Following is my code:</p>
  
  <pre><code>BufferedWriter fileOut;
  
  String itsFileLocation = "c:\\program files\\test\\"
      System.out.println(itsFileLocation);
      try {
       fileOut = new BufferedWriter(new FileWriter("C:\\test.bat"));
       fileOut.write("cd\\"+"\n");
       fileOut.write("cd "+ itsFileLocation +"\n");
       fileOut.write("test.exe"+"\n");
       fileOut.write("exit"+"\n");
       
       fileOut.close(); // Close the output stream after all output is done.
      } catch (IOException e1) {
       e1.printStackTrace();
      } // Create the Buffered Writer object to write to a file called filename.txt
      Runtime runtime = Runtime.getRuntime();
      try {
       Process process =runtime.exec("cmd /c start C:\\test.bat");
      } catch (IOException e) {
       e.printStackTrace();
      }
  </code></pre>
  
  <p>The above code works perfectly.  However,  the command prompt is also opened at the back of my .exe (Application).  It closes only after the .exe file exits..</p>
  
  <p>I need to clse my command prompt when my application stats.</p>
  
  <p>My .bat file will be like following after it is written by the program.</p>
  
  <pre><code>cd\
  cd C:\Program Files\test\
  test.exe
  exit
  </code></pre>