compile Java Source File - Java javax.tools

Java examples for javax.tools:JavaCompiler

Description

compile Java Source File

Demo Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.tools.DiagnosticListener;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class Main{
    public static void main(String[] argv) throws Exception{
        String completeClassName = "java2s.com";
        String source = "java2s.com";
        System.out.println(compileSource(completeClassName,source));
    }//from   w w w  . j  a  v a2 s.  c o m
    private static final Map<String, JavaFileObject> output = new ConcurrentHashMap<String, JavaFileObject>();
    public static ClassLoader classLoader = new CompilerClassLoader(
            CompilerUtils.class.getClassLoader());
    private static final Map<String, Class<?>> classCache = new ConcurrentHashMap<String, Class<?>>();
    public static Class<?> compileSource(String completeClassName,
            String source) throws Throwable {
        boolean result = false;
        JavaFileManager fileManager = CompilerUtils
                .getStringSourceJavaFileManager(compiler, null, null,
                        Charset.forName("UTF-8"));
        try {
            CompilationTask task = compiler.getTask(null, fileManager,
                    null, null, null, Arrays
                            .asList(new JavaSourceFromString(
                                    completeClassName, source)));
            result = task.call();
        } finally {
            fileManager.close();
        }

        if (!result)
            return null;

        return getClassByName(completeClassName);
    }
    public static JavaFileManager getStringSourceJavaFileManager(
            JavaCompiler compiler,
            DiagnosticListener<? super JavaFileObject> diagnosticListener,
            Locale locale, Charset charset) {

        JavaFileManager fileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(
                compiler.getStandardFileManager(diagnosticListener, locale,
                        charset)) {
            @Override
            public JavaFileObject getJavaFileForOutput(Location location,
                    String className, Kind kind, FileObject sibling)
                    throws IOException {
                JavaFileObject jfo = new ByteJavaObject(className, kind);
                output.put(className, jfo);
                return jfo;
            }
        };
        return fileManager;
    }
    public static Class<?> getClassByName(String name)
            throws ClassNotFoundException {
        Class<?> ret = classCache.get(name);
        if (ret != null)
            return ret;

        synchronized (classCache) {
            ret = classCache.get(name);
            if (ret != null)
                return ret;

            ret = Class.forName(name, false, classLoader);
            classCache.put(name, ret);
            return ret;
        }
    }
}

Related Tutorials