title: Creating a ".bat" file, run it and delete it from Java
body: <p>I'm making a <code>.bat</code> dynamically in <code>Java</code>.</p>
  
  <p>After creating the file and exectuing it, I want to delete it.</p>
  
  <p>In order to make sure that <code>delete()</code> will be executing after running the <code>.bat</code> file, I delete the file within a different thread.</p>
  
  <p>Is this a good approach for this task? How would you implement it in a better way? </p>
  
  <pre><code>final File file = new File("run.bat");
  file.createNewFile();
  PrintWriter writer = new PrintWriter(file, "UTF-8");
  writer.println(COMMAND1);
  writer.println(COMMAND2);
  
  .... ANOTHER COMMANDS
  
  writer.close();
  Runtime.getRuntime().exec("cmd /c start a.bat");
  
  new Thread(new Runnable() {
      public void run() {
          file.delete();
      }
  }).run();
  </code></pre>