title: How Do I Compile/Run a java program from a Java Program (Input failure?)
body: <p><strong>Background info:</strong></p>
  
  <p>I am a high school student who is currently learning Java and as so if my code has an obvious flaw in it/ I accidentally reinvent the wheel with the code, I apologize.</p>
  
  <p>Recently I have been working on writing an esoteric language and decided that I wanted to write it as an interpreter that translates the code to Java and then ran the code. My first step towards this was an attempt to create a mini-program that compiled and ran a java program. Most of the code from that was scrounged from another article, which is the third or fourth article I've looked threw:
  <a href="https://stackoverflow.com/questions/4842684/how-to-compile-run-java-program-in-another-java-program">how to compile &amp; run java program in another java program?</a></p>
  
  <p>I used the code from the third answer on that thread and initially thought that it worked. Unfortunately, when I tried running the code using the filename of the class for the program to be compiled and run within itself, the program failed.</p>
  
  <p><strong>Here is the modified code:</strong></p>
  
  <pre><code> /**
   *Functions printLines, Run, and parts of main came from stacks overflow 
   *originaly but modifications have been made
   *https://stackoverflow.com/questions/4842684/how-to-compile-run-java-program-in-another-java-program
   */
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.util.Scanner;
  
  public class JTest
  {
          private static void printLines(String name, InputStream ins) throws Exception 
      {
              String line = null;
          BufferedReader in = new BufferedReader(new InputStreamReader(ins));
              while ((line = in.readLine()) != null) 
              {
                  //System.out.println(name + " " + line);
                  System.out.println(line);
              }
      }
  private static int run(String command) throws Exception 
      {
          System.out.println(command);//prints command
              Process pro = Runtime.getRuntime().exec(command);
              printLines(command, pro.getInputStream());
              printLines(command + " stderr:", pro.getErrorStream());
              pro.waitFor();
          // System.out.println(command + " exitValue() " + pro.exitValue());
              return pro.exitValue();
      }
      public static void main(String args[]) 
      {
      System.out.println("Enter the name of the file you want to run: ");
      Scanner cin = new Scanner(System.in);  
          String jFileName = cin.nextLine();  
  
          try 
          {
              int k =  run("javac " + jFileName + ".java");
              if (k==0)
              k=run("java " + jFileName);
          } 
          catch (Exception e) 
          {
              e.printStackTrace();
          }
      }  
  }
  </code></pre>
  
  <p><strong>I also used another class:</strong></p>
  
  <pre><code>public class Cout
  {
      public static void main(String args [])
      {
          System.out.println("Hello World");
      }
  }
  </code></pre>
  
  <p><strong>In my initial test...</strong></p>
  
  <p>Output:</p>
  
  <pre><code>Enter the name of the file you want to run:
  </code></pre>
  
  <p>Input:</p>
  
  <pre><code>Cout
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>javac Cout.java
  java Cout
  Hello World
  </code></pre>
  
  <p><strong>Here's what Happened when I tried to run JTest from JTest...</strong></p>
  
  <p>Output:</p>
  
  <pre><code>Enter the name of the file you want to run:
  </code></pre>
  
  <p>Input:</p>
  
  <pre><code>JTest
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>javac JTest.java
  java JTest
  Enter the name of the file you want to run:
  </code></pre>
  
  <p>Input:</p>
  
  <pre><code>Cout
  </code></pre>
  
  <p>After I entered this, nothing more was outputted onto the terminal window which leads to my main question:</p>
  
  <p><strong>Why didn't my code run the Cout class and how do I fix it? (Preferably in a way that makes my code compatible with both linux and windows) Or is there a resource someone could point me towards?</strong></p>