import java.io.*; import java.net.*; import java.lang.reflect.*; public class javaShell { //-------------------------------------------------------------------------------- public static void main( String[] args ) throws Exception { String javaHome = System.getProperty( "java.home" ); File tools = new File( javaHome + "/../lib/tools.jar" ); if( !tools.exists() ) { System.out.println( "tools.jar not found." ); return; } String classStr = "public class otf { public String toString() { try { CODE } catch( Throwable e ) { e.printStackTrace(); } return \"\"; } }"; BufferedReader reader = new BufferedReader( new InputStreamReader( System.in, "ISO_8859_1" ) ); StringBuffer buf = new StringBuffer(); while( true ) { String line = reader.readLine(); if( line == null || line.equalsIgnoreCase( "quit" ) || line.equalsIgnoreCase( "exit" ) ) { break; } else if( line.equals( "" ) ) { exec( classStr, buf.toString() ); buf = new StringBuffer(); } buf.append( line ); } } //-------------------------------------------------------------------------------- private static void exec( String classStr, String userStr ) throws Exception { //Generate java source String className = "otf_" + System.currentTimeMillis(); String javaFileName = className + ".java"; classStr = replaceAll( classStr, "CODE", userStr ); classStr = replaceAll( classStr, "public class otf", "public class " + className ); saveStringToFile( classStr, javaFileName ); //Compile String javaHome = System.getProperty( "java.home" ); File tools = new File( javaHome + "/../lib/tools.jar" ); String toolsFileName = tools.getCanonicalPath(); URL toolsURL = new URL( "file:" + toolsFileName ); URLClassLoader cl = new URLClassLoader( new URL[]{ toolsURL } ); Class javac = cl.loadClass( "com.sun.tools.javac.Main" ); Method m = javac.getDeclaredMethod( "compile", new Class[]{ String[].class } ); String[] args = new String[]{ javaFileName }; Object result = m.invoke( null, new Object[]{ args } ); int resultInt = ( ( Integer )result ).intValue(); if( resultInt != 0 ) { System.out.println( "Compilation error." ); return; } //Execute Class otfClass = cl.loadClass( className ); Object o = otfClass.newInstance(); o.toString(); } //-------------------------------------------------------------------------------- public static String replaceAll( String target, String from, String to ) { int len = from.length(); if( len == 0 ) { return target; } StringBuffer buf = new StringBuffer( target.length() ); while( true ) { int pos = target.indexOf( from ); if( pos == -1 ) { break; } buf.append( target.substring( 0, pos ) ); buf.append( to ); target = target.substring( pos + len ); } buf.append( target ); return buf.toString(); } //-------------------------------------------------------------------------------- public static void saveStringToFile( String str, String fileName ) throws IOException { OutputStream os = new FileOutputStream( fileName ); try { os.write( str.getBytes( "ISO-8859-1" ) ); } finally { os.close(); } } // -------------------------------------------------------------------------------- }