Home | Gaming | Programming | Play Online | Contact | Keyword Query
Games++ Games & Game Programming

GAMES++
Games++ Home
Games++ Gaming
Games++ Programming
Beta Testing Games
Free Online Games
Hints & Cheats

BROWSER UTILITIES
E-mail This Page
Add to Favorites

SITE SEARCH

Web Games++

AFFILIATES
Cheat Codes
Trickster Wiki
Game Ratings
Gameboy Cheats
PlayStation Cheats
BlackBerry Games
Photoshop Tutorials
Illustrator Tutorials
ImageReady Tutorials

ADVERTISEMENT

ADVERTISEMENT

How To Dynamically Create, Compile And Run

Java How To: Source Code

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

public class MakeTodayClass 
{
   Date   today       = new Date();
   String todayMillis = Long.toString(today.getTime());
   String todayClass  = "z_" + todayMillis;
   String todaySource = todayClass + ".java";
  
   public static void main (String args[])
   {
      MakeTodayClass mtc = new MakeTodayClass();
      mtc.createIt();

      if (mtc.compileIt()) 
      {
         System.out.println("Running " + mtc.todayClass + ":\n\n");
         mtc.runIt();
      }
      else
         System.out.println(mtc.todaySource + " is bad.");
   }

   public void createIt() 
   {
      try 
      {
         FileWriter aWriter = new FileWriter(todaySource, true);
         aWriter.write("public class "+ todayClass + "{");
         aWriter.write(" public void doit() {");
         aWriter.write(" System.out.println(\""+todayMillis+"\");");
         aWriter.write(" }}\n");
         aWriter.flush();      
         aWriter.close();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
  
   public boolean compileIt() 
   {
      String [] source = { new String(todaySource)};
      ByteArrayOutputStream baos= new ByteArrayOutputStream();

      new sun.tools.javac.Main(baos,source[0]).compile(source);
    
      // if using JDK >= 1.3 then use
      // public static int com.sun.tools.javac.Main.compile(source);    
      return (baos.toString().indexOf("error")==-1);
   }
    
   public void runIt() 
   {
      try 
      {
         Class  params[]    = {};
         Object paramsObj[] = {};
         Class  thisClass   = Class.forName(todayClass);
         Object iClass      = thisClass.newInstance();
         Method thisMethod  = thisClass.getDeclaredMethod("doit", params);

         thisMethod.invoke(iClass, paramsObj);
      }
      catch (Exception e) 
      {
         e.printStackTrace();
      }
   }
}

Copyright © 1998-2007, Games++ All rights reserved. | Privacy Policy