简体   繁体   中英

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?

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.

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

JavaCompiler是javax.tools.JavaCompiler ,它实现了javax.tools.Tool接口,请参阅javadoc for javax.tools.Tool#run方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM