Created
May 24, 2013 20:11
-
-
Save bchetty/5646188 to your computer and use it in GitHub Desktop.
Compile Java files at runtime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.util.Arrays; | |
import java.util.Locale; | |
import javax.tools.Diagnostic; | |
import javax.tools.DiagnosticCollector; | |
import javax.tools.JavaCompiler; | |
import javax.tools.JavaFileObject; | |
import javax.tools.StandardJavaFileManager; | |
import javax.tools.ToolProvider; | |
/** | |
* | |
* @author Babji Prashanth, Chetty | |
*/ | |
public class JCompiler { | |
public static void main(String[] args) throws IOException { | |
File dir = new File("directory-path"); | |
File[] javaFiles = dir.listFiles( | |
new FilenameFilter() { | |
public boolean accept(File file, String name) { | |
return name.endsWith(".java"); | |
} | |
}); | |
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); | |
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); | |
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnostics, Locale.GERMANY, Charset.forName("UTF-8")); | |
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(javaFiles)); | |
javaCompiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call(); | |
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { | |
System.out.format("Error on line %d in %d%n", diagnostic.getLineNumber(), diagnostic.getSource().toString()); | |
} | |
fileManager.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment