javax systemcompiler不生成类文件

时间:2015-11-06 09:45:17

标签: java jar javac java-compiler-api

这是我编译一些java源文件的代码:

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    dependencies = getJarFiles(this.libPath);
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

    ArrayList<File> sourceFiles = getSourceFiles(this.apiGeneratedSrcPath);
    Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
    try {
        fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(this.genClassDir)));
        fileManager.setLocation(StandardLocation.CLASS_PATH, dependencies);
        if(!compiler.getTask(null, fileManager, null, null, null, compilationUnits1 ).call())
            throw new IllegalStateException("Error on compiling API");
        fileManager.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

我也试过这个版本:

    optionList.addAll(Arrays.asList("-Xmaxerrs", "20"));
    optionList.addAll(Arrays.asList("-d", "target\\classes"));
    optionList.addAll(Arrays.asList("-cp", classpath));


    //Java files
    System.out.println("Get source files");
    ArrayList<File> sourcefiles =  getSourceFiles(this.apiGeneratedSrcPath);
    Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourcefiles);
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1 );

    System.out.println("Compile API");
    boolean compiled = task.call();
    if(!compiled)
        throw new IllegalStateException("Error on compiling API");
    System.out.println("Compile API Done");

但是在我的genClassDir(应该为我的类文件输出)中没有生成类文件,仍然没有编译器错误。

我也收到htis警告我无法解决,但它应该打破构建过程:

Access restriction: The method 'StandardJavaFileManager.setLocation(JavaFileManager.Location, Iterable<? extends File>)' is not API (restriction on required library 'C:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar')

为什么编译器不会从我的.java源中生成.class文件的任何想法?

1 个答案:

答案 0 :(得分:0)

我不应该把它写成答案,但在评论中我无法正确格式化代码。这是我做的,它的工作

package hello;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;

public class Snippet {

    public static void main(String[] args) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        List<File> sourceFiles = Arrays.asList(new File("C://project//src//hello//Hello.java"));
        Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
        try {
            fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("C://project//classes//")));
            if(!compiler.getTask(null, fileManager, null, null, null, compilationUnits1 ).call())
                throw new IllegalStateException("Error on compiling API");
            fileManager.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

和Hello.java

package hello;

public class Hello {

}

输出是一个类文件:C://project//classes//hello//Hello.class