我在网上找到了这段关于 JavaCompiler 的代码
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null,
"src/org/kodejava/example/tools/Hello.java");
对于 compiler.run(null, null, null
) 来说,这三个 null 是默认的 System.in
、System.out
和 System.err
分别。但是这些值实际上是做什么的,因为我找不到这些值的 API?有人可以解释一下吗?
最佳答案
Javadoc 是 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
对于 System.in、System.out 和 System.err,这些是(默认情况下)连接到 STDIN、STDERR 和 STDOUT 的全局流。这三者是操作系统在JVM启动时设置的。您可以将它们通过管道传输到文件,或者它们只是写入(读取)控制台。
在这种情况下,您将使用参数来检查程序的编译器输出(而不是仅仅将其发送给用户)。这就是“以某种未指定的格式写出的诊断”的用武之地。
关于Java 编译器 run() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11626873/