javacompilationjava-compiler-api

Java Compiler run() method


I found this code online about the JavaCompiler

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null,
             "src/org/kodejava/example/tools/Hello.java");

And it says for the compiler.run(null, null, null that these three nulls are the default System.in, System.out and System.err respectively. But what do these values actually do as I cannot find the API for these? Could someone please explain?


Solution

  • The Javadoc is here.

    int run(InputStream in, OutputStream out, OutputStream err, String... arguments)

    Run the tool with the given I/O channels and arguments. By convention a tool returns 0 for success and nonzero for errors. Any diagnostics generated will be written to either out or err in some unspecified format.

    Parameters:
        in - "standard" input; use System.in if null
        out - "standard" output; use System.out if null
        err - "standard" error; use System.err if null
        arguments - arguments to pass to the tool 
    Returns:
        0 for success; nonzero otherwise
    

    As for System.in, System.out, and System.err those are global streams that (by default) connect to STDIN, STDERR, and STDOUT. These three are set up by the operating system when the JVM starts. You can pipe them to files, or they just write to (read from) the console.

    In this case, you would use the parameters to inspect the compiler output from your program (rather than just sending it out to the user). This is where the "diagnostics written out in some unspecified format" come in.