How to compile multiple Java classes at a time using the JavaCompiler API

I am new to java and I am following this tutorial as it is very informative and explains everything in great detail. The bottom of the tutorial explains how the JavaFileManager can be used to compile several java files and gives some examples of this, but I still can't get it to compile multiple files.

Another problem is that in this example it only ever shows how to compile one java file (which I can already work with), but these are several files that I had a problem with, since I want to be able to compile projects consisting from multiple java classes on my own system

This is what I have at the moment:

public static void main(String[] args) throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // Line 1. MyDiagnosticListener listener = new MyDiagnosticListener(); // Line 2. StandardJavaFileManager fileManager = compiler.getStandardFileManager( listener, null, null); // Line 3. String fileToCompile = "test" + File.separator + "ManyErrors.java"; // Line 4 Iterable fileObjects = fileManager.getJavaFileObjectsFromStrings(Arrays .asList(fileToCompile)); // Line 5 CompilationTask task = compiler.getTask(null, fileManager, listener, null, null, fileObjects); // Line 6 Boolean result = task.call(); // Line 7 if (result == true) { System.out.println("Compilation has succeeded"); } } class MyDiagnosticListener implements DiagnosticListener { public void report(Diagnostic diagnostic) { System.out.println("Code->" + diagnostic.getCode()); System.out.println("Column Number->" + diagnostic.getColumnNumber()); System.out.println("End Position->" + diagnostic.getEndPosition()); System.out.println("Kind->" + diagnostic.getKind()); System.out.println("Line Number->" + diagnostic.getLineNumber()); System.out.println("Message->" + diagnostic.getMessage(Locale.ENGLISH)); System.out.println("Position->" + diagnostic.getPosition()); System.out.println("Source" + diagnostic.getSource()); System.out.println("Start Position->" + diagnostic.getStartPosition()); System.out.println("\n"); } 
+4
source share
1 answer

getJavaFileObjectsFromStrings from StandardJavaFileManager accepts an Iterable<String> .

This means that you can just pass it any iterable collection of strings to get Iterable<? extends JavaFileObject> Iterable<? extends JavaFileObject> , which, in turn, is passed to the getTask method of any class that implements the JavaCompiler interface.


This is not related to the answer, but I would like to add that you are probably going the wrong way if your goal is to get to know Java. Procedural compilation of Java classes is a rather complicated topic, and it seems that you didn’t understand the code that you published completely, because the answer to your question is right there: calling Arrays.asList(fileToCompile) creates an array of strings with exactly one line in this ; even without documentation, it should be easy to get getJavaFileObjectsFromStrings accepts an array of strings matching file names. So I really would not try to go down this road, but I first got acquainted with the Java documentation and simpler concepts. Especially if you are not familiar with the concepts of OO.

+2
source

Source: https://habr.com/ru/post/1416632/


All Articles