Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package org.apache.pig.impl.util;
public class JavaCompilerHelper {
    private static final Logger LOG = Logger.getLogger(JavaCompilerHelper.class);
This class allows code to be generated directly from a String, instead of having to be on disk.
    public static class JavaSourceFromString extends SimpleJavaFileObject {
        final String code;
        public JavaSourceFromString(String nameString code) {
            super(URI.create("string:///" + name.replace('.','/') + ..), .);
            this. = code;
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return ;
    private String classPath;
    public JavaCompilerHelper() {
        this. = System.getProperty("java.class.path");
    public void compile(String targetJavaSourceFromString... sources) {
        .info("compiling java classes to "+target);
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaFileManager fileManager = compiler.getStandardFileManager(nullnullnull);
        DiagnosticCollector<JavaFileObjectdiagnostics = new DiagnosticCollector<JavaFileObject>();
        List<StringoptionList = Lists.newArrayList();
        // Adds the current classpath to the compiler along with our generated code
        if (!compiler.getTask(nullfileManagerdiagnosticsoptionListnull, Arrays.asList(sources)).call()) {
            .warn("Error compiling Printing compilation errors and shutting down.");
            for (Diagnostic<? extends JavaFileObjectdiagnostic : diagnostics.getDiagnostics()) {
                .warn("Error on line " + diagnostic.getLineNumber() + ": " + diagnostic.getMessage(.));
            throw new RuntimeException("Unable to compile");
    public void addToClassPath(String path) {
        this. = this. + System.getProperty("path.separator") + path;
    public String getClassPath() {
        return ;
New to GrepCode? Check out our FAQ X