简体   繁体   English

Java Compiler run()方法

[英]Java Compiler run() method

I found this code online about the JavaCompiler 我在网上找到了关于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? 它说为compiler.run(null, null, null ,这三个空值分别是默认的System.inSystem.outSystem.err 。但这些值实际上做了什么,因为我找不到这些的API?有人可以解释一下吗?

The Javadoc is here . Javadoc就在这里

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

Run the tool with the given I/O channels and arguments. 使用给定的I / O通道和参数运行该工具。 By convention a tool returns 0 for success and nonzero for errors. 按照惯例,工具返回0表示成功,非零表示错误。 Any diagnostics generated will be written to either out or err in some unspecified format. 生成的任何诊断都将以某种未指定的格式写入out或err。

 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. 对于System.in,System.out和System.err,这些是全局流(默认情况下)连接到STDIN,STDERR和STDOUT。 These three are set up by the operating system when the JVM starts. 这三个是在JVM启动时由操作系统设置的。 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. 例如,如果要将输出重定向到某个文件,可以创建文件输出流,然后传递给#run调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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