title: Writing and implementing new Java class files during run-time
body: <p>Is it possible to somehow 'import' a new Java class into a running program and make use of it?</p>
  
  <p>Could I have a program create a new File of type '.java' and then include it in the project files and reference it without having to restart the program?</p>
  
  <p>The following is an example of what I mean:</p>
  
  <pre><code>import java.io.*;
  
  public class Program {
  
  File JClass = new File("JClass.java");
  public static BufferedWriter out = null;
  
      public static void main(String[] args) {
          try {
              out = new BufferedWriter(new FileWriter("JClass.java"));
              out.write("public abstract class JClass {");
              out.newLine();
              out.newLine();
              out.write("    public void printSomething(String a) {");
              out.newLine();
              out.write("        System.out.println(a);");
              out.newLine();
              out.write("    }");
              out.newLine();
              out.write("}");
              out.close();
          } catch (IOException e)
          {
              System.exit(-1);
          }
  
          //Somehow import JClass.java as a class here
  
          JClass.printSomething("Yay! It worked!");
      }
  
  }
  </code></pre>
  
  <p>Resulting 'JClass.java' file:</p>
  
  <pre><code>public abstract class JClass {
  
      public void printSomething(String a) {
          System.out.println(a);
      }
  }
  </code></pre>
  
  <p>Similarly, would it be possible to create a copy of one of the project's source files, edit the code in the file, and then somehow force the changes upon the running program?</p>
  
  <p>I do not care so much about practical application at this point. I am merely exploring different ideas I have had relating to programming. I also understand that this is potential for all sorts of disaster. Editing running code, and including classes on the fly (which I don't imagine would be checked for errors as the other classes are when the project is built), could have very unpredictable results. I merely want to play around with the idea.</p>
  
  <p>That said, if anyone has any HELPFUL warnings or things to look out for, I would appreciate them. Otherwise, I would appreciate if people kept from responding 'This is a bad idea' or 'there are easier and better ways to solve problems'. I am NOT trying to solve a problem with this. I am only exploring the idea.</p>
  
  <p>So, is this possible?</p>