Java 系统编译器不生成类文件



这是我编译一些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 FilesJavajdk1.7.0_80jrelibrt.jar')

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

我本不应该把它写成答案,但在注释中,我无法正确格式化代码。以下是我所做的,它起到了的作用

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

相关内容

  • 没有找到相关文章

最新更新