title: How Do I Compile And Run A .java File In Java?
body: <p>I tried this but it didin't work.
  My method to launch command prompt commands:</p>
  
  <pre><code>public static void executeCommand(String command, String path){
    File folder = new File(path);
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.directory(folder.getAbsoluteFile()); 
    pb.redirectErrorStream(true);
    try{
      pb.start();
    }catch (IOException e){
      e.printStackTrace();
    }
  }  
  </code></pre>
  
  <p>`</p>
  
  <p>and the code to call the methods:</p>
  
  <pre><code>executeCommand("javac "+str+".java", path);
  executeCommand("java "+str, path);
  </code></pre>
  
  <p>But it's throwing an IOException. I was wondering if this was the wrong way to do it and how I can fix it. "str" is the .java file. The path is the path of the folder to run it in.</p>