This is my code to compile some java sourcefiles:
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();
}
and i also tried this version:
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");
But in my genClassDir (which should be output for my class files) there are no class files generated, still i dont get a compiler error.
I am also getting htis warning which i cannot resolve but it should break the build process:
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')
Any ideas why the compiler dont geenerate .class files from my .java sources??
I should not have written this as answer but in comments I cannot format the code properly. Here is what I did and it worked
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();
}
}
}
And Hello.java
package hello;
public class Hello {
}
And the output was a class file : C://project//classes//hello//Hello.class