javac 可以从 stdin 编译吗?
javac 可以从 stdin 编译吗? 像这样的东西:
cat myfile | javac
Can javac compile from stdin?
Something like this :
cat myfile | javac
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,没有选择这样做。
来自文档:
No, there is no option to do that.
From the documentation:
您可以尝试使用
/dev/stdin
作为源文件(然后,您需要找到强制javac
将其视为 Java 源代码的选项)。但我只是制作一个 shell 脚本,将标准输入放在一些后缀为
.java
的临时文件上。但我认为(我不确定)
javac
确实希望在名为Foo.java
的文件中定义一个类Foo
。You might try with
/dev/stdin
as the source file (and then, you'll need to find the option which forcesjavac
to consider it as Java source code).But I would just make a shell script which put the stdin on some temporary file suffixed with
.java
.But I think (I am not sure) that
javac
really wants a classFoo
to be defined inside a file namedFoo.java
.对于普通的 javac 来说这是不可能的 - 原因是大多数 Java 程序由多个类组成,这些类通常也分布在多个源文件(编译单元)上。
您也许可以构建一个工具来执行此操作,使用 Java 编译器 API 进行实际编译。
您必须创建一个 JavaFileManager,它通过标准输入中的文本模拟文件,并将其传递给编译器。
This is not possible with plain javac - the reason is that most Java programs consist of more than one class, which are usually also distributed over more than one source file (compilation unit).
You can probably build a tool which does this, using the Java compiler API for the actual compilation.
You would have to create a JavaFileManager which simulates files by the text from standard input, and pass this to the compiler.
您无法使用 Sun java 做到这一点,但您可以编写一个脚本来处理 stdin 到 javac 可以理解的内容的转换。
像这样的 Python 脚本:
请注意,该脚本将在当前目录中创建一个类名的文件。最好使用 /tmp 中的某些内容,但请记住它必须与类命名相同。如果您正在测试脚本,请不要执行以下操作:
You can't do it with Sun java, but you can write a script to handle the conversion of stdin to something javac can understand.
Something like this Python script:
Note that the script will create a file of the name of the class in the current directory. It's probably better to use something in /tmp, but keep in mind that it has to be named the same as the class. If you are testing the script don't do something like this: