public static Class compile(final String className, final String classString) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

        JavaFileManager fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null));

        JavaFileObject file = new CharSequenceJavaFileObject(className, classString);

        Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
        CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);

        boolean success = task.call();
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            System.out.println(diagnostic.getCode());
            System.out.println(diagnostic.getKind());
            System.out.println(diagnostic.getPosition());
            System.out.println(diagnostic.getStartPosition());
            System.out.println(diagnostic.getEndPosition());
            System.out.println(diagnostic.getSource());
            System.out.println(diagnostic.getMessage(null));
        }

        if (success) {
            Class result = null;

            try {
                result = fileManager.getClassLoader(null).loadClass(className);
            } catch (ClassNotFoundException e) { }

            return result;
        }

        return null;
    }