Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

You can specify your own streams instead of relying on standard System streams. E.g., if you want to redirect an output to some file, you can create File output stream, and pass to #run call.

JavaCompiler is javax.tools.JavaCompiler and it implements javax.tools.Tool interface, see javadoc for javax.tools.Tool#run method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top