public Map < String , byte [ ] > compile ( String fileName , String source , Writer err , String sourcePath , String classPath ) { // to collect errors, warnings etc. DiagnosticCollector < JavaFileObject > diagnostics = new DiagnosticCollector < JavaFileObject > ( ) ; // create a new memory JavaFileManager MemoryJavaFileManager manager = new MemoryJavaFileManager ( stdManager ) ; // prepare the compilation unit List < JavaFileObject > compUnits = new ArrayList < JavaFileObject > ( 1 ) ; compUnits . add ( manager . makeStringSource ( fileName , source ) ) ; // javac options List < String > options = new ArrayList < String > ( ) ; options . add ( "-Xlint:all" ) ; // options.add("-g:none"); options . add ( "-deprecation" ) ; if ( sourcePath != null ) { options . add ( "-sourcepath" ) ; options . add ( sourcePath ) ; } if ( classPath != null ) { options . add ( "-classpath" ) ; options . add ( classPath ) ; } // create a compilation task javax . tools . JavaCompiler . CompilationTask task = tool . getTask ( err , manager , diagnostics , options , null , compUnits ) ; if ( task . call ( ) == false ) { PrintWriter perr = new PrintWriter ( err ) ; for ( Diagnostic diagnostic : diagnostics . getDiagnostics ( ) ) { perr . println ( diagnostic . getMessage ( null ) ) ; } perr . flush ( ) ; return null ; } Map < String , byte [ ] > classBytes = manager . getClassBytes ( ) ; try { manager . close ( ) ; } catch ( IOException exp ) { } return classBytes ; }
compile given String source and return bytecodes as a Map .