Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to compile multiple Java classes in one go using the JavaCompiler API
    primarykey
    data
    text
    <p>I am new to java and I am following <a href="http://www.javabeat.net/2007/04/the-java-6-0-compiler-api/" rel="nofollow">this tutorial</a> as it is very informative and explains everything in great detail. At the bottom of the tutorial it explains how a JavaFileManager can be used to compile multiple java files and gives a few examples of this but i still cant get it to compile multiple files myself</p> <p>Another problem is that in the example it only ever shows how to compile one java file (which i can already get working) but it is the multiple files that i am having a problem with as i want to be able to compile projects made up of multiple java classes in my own system</p> <p>This is what i have to the moment:</p> <pre><code>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-&gt;" + diagnostic.getCode()); System.out.println("Column Number-&gt;" + diagnostic.getColumnNumber()); System.out.println("End Position-&gt;" + diagnostic.getEndPosition()); System.out.println("Kind-&gt;" + diagnostic.getKind()); System.out.println("Line Number-&gt;" + diagnostic.getLineNumber()); System.out.println("Message-&gt;" + diagnostic.getMessage(Locale.ENGLISH)); System.out.println("Position-&gt;" + diagnostic.getPosition()); System.out.println("Source" + diagnostic.getSource()); System.out.println("Start Position-&gt;" + diagnostic.getStartPosition()); System.out.println("\n"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload