Question

I am new to java and I am following this tutorial 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

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

This is what i have to 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");
}
Était-ce utile?

La solution

getJavaFileObjectsFromStrings from StandardJavaFileManager takes an Iterable<String>.

This means you can just pass it any iterable collection of strings in order to get an 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'd like to add that you are probably going down the wrong path, if your goal is to familiarize yourself with Java. Procedural compilation of Java classes is a rather advanced topic, and it doesn't seem you understood the code you posted fully, because the answer to your question is right within it: the Arrays.asList(fileToCompile) call creates an array of strings with exactly one string in it; even without documentation, it should be easy to deduce getJavaFileObjectsFromStrings takes an array of strings corresponding to filenames. So I really wouldn't try going down that road, but rather, I'd familiarize myself with Java documentation and simpler concepts first. Especially if you are not familiar with OO concepts.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top