use of javax.tools.StandardJavaFileManager in project narchy by automenta.
the class Compiler method compil.
/**
* Compile a class
*
* @param classCompleteName
* Class to compile
* @param code
* Code of class
* @return Directory where lies the compiled files
* @throws IOException
* On read/write issue
*/
public static File compil(final String classCompleteName, final String code) throws IOException {
final File directory = new File(UtilIO.obtainTemporaryDirectory(), UtilText.concatenate("Compiler", File.separator));
Debug.println(DebugLevel.INFORMATION, "Compile in ", directory.getAbsolutePath());
// Create the compiler
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// Create the list of class to compile
final ArrayList<JavaSourceFromString> compilationUnits = new ArrayList<JavaSourceFromString>();
compilationUnits.add(new JavaSourceFromString(classCompleteName, code));
// We create the file manager, and says the directory to use for output
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
UtilIO.createDirectory(directory);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(directory));
// Do the compilation itself
if (compiler.getTask(null, fileManager, null, null, null, compilationUnits).call() == false) {
throw new RuntimeException(UtilText.concatenate("Compilation of ", classCompleteName, " failed !"));
}
fileManager.flush();
fileManager.close();
return directory;
}
use of javax.tools.StandardJavaFileManager in project GIPC by pdewan.
the class MyDynamicCompilation method compileSourceFile.
public static void compileSourceFile(File sourceFile) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("/temp")));
// Compile the file
CompilationTask compilationTask = compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile)));
compilationTask.call();
fileManager.close();
// delete the source file
// sourceFile.deleteOnExit();
}
use of javax.tools.StandardJavaFileManager in project fakereplace by fakereplace.
the class ClassLoaderCompiler method compile.
public void compile() {
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standard = compiler.getStandardFileManager(null, null, null);
JavaCompiler.CompilationTask task = compiler.getTask(null, new ClassLoaderJavaFileManager(standard), null, null, null, classBaseNames.stream().map((name) -> new LocalFileObject(base.resolve(name.replace(".", "/") + ".java"), name, Kind.SOURCE)).collect(Collectors.toList()));
if (!task.call()) {
throw new RuntimeException("Compile failed");
}
}
use of javax.tools.StandardJavaFileManager in project st-js by st-js.
the class CommandLine method compile.
static void compile(final String path, final List<File> sourceFiles, List<File> dependencies) {
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new STJSRuntimeException("A Java compiler is not available for this project. " + "You may have configured your environment to run with a JRE instead of a JDK");
}
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.CLASS_PATH, dependencies);
Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
fileManager.close();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of javax.tools.StandardJavaFileManager in project Payara by payara.
the class JavaClientGenerator method compileSources.
private void compileSources() {
try {
List<File> files = new ArrayList<File>();
gatherFiles(baseDirectory, files);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<String> options = new ArrayList<String>();
options.add("-cp");
StringBuilder sb = new StringBuilder();
sb.append(ASClassLoaderUtil.getModuleClassPath(habitat, "", null));
options.add(sb.toString());
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
if (!compiler.getTask(null, fileManager, null, options, null, compilationUnits).call()) {
RestLogging.restLogger.log(Level.INFO, RestLogging.COMPILATION_FAILED);
}
fileManager.close();
} catch (IOException ex) {
RestLogging.restLogger.log(Level.SEVERE, null, ex);
}
}
Aggregations