javac 可以从 stdin 编译吗?

发布于 2024-12-14 03:47:35 字数 80 浏览 2 评论 0原文

javac 可以从 stdin 编译吗? 像这样的东西:

cat myfile | javac

Can javac compile from stdin?
Something like this :

cat myfile | javac

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的故事 2024-12-21 03:47:35

不,没有选择这样做。

来自文档

将源代码文件名传递给 javac 有两种方法:

  • 对于少量源文件,只需在命令行上列出文件名即可。
  • 对于大量源文件,请列出文件中的文件名,并以空格或换行符分隔。然后在 javac 命令行上使用列表文件名,前面带有 @ 字符。

源代码文件名必须具有 .java 后缀,类文件名必须具有 .class 后缀,源文件和类文件都必须具有标识类的根名称 。例如,名为 MyClass 的类将写入名为 MyClass.java 的源文件中,并编译为名为 MyClass.class 的字节码类文件。

No, there is no option to do that.

From the documentation:

There are two ways to pass source code file names to javac:

  • For a small number of source files, simply list the file names on the command line.
  • For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.

Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.

乄_柒ぐ汐 2024-12-21 03:47:35

您可以尝试使用 /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 forces javac 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 class Foo to be defined inside a file named Foo.java.

萝莉病 2024-12-21 03:47:35

对于普通的 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.

南薇 2024-12-21 03:47:35

您无法使用 Sun java 做到这一点,但您可以编写一个脚本来处理 stdin 到 javac 可以理解的内容的转换。

像这样的 Python 脚本:

import fileinput, re, subprocess

class_name = None

buffer = []
class_matcher = re.compile('\w+ class (?P<name>\w+)')

for line in fileinput.input():
    if None == class_name:
        buffer.append(line)
        m = class_matcher.match(line)
        if m:
            class_name = m.group('name')
            print "found %s" % class_name
            file_name = '%s.java' % class_name
            out = open(file_name, 'wb')
            out.writelines(buffer)
    else:
        out.write(line)

if None == class_name:
    print "Error: no class found"

else:
    out.close()

    print 'javac %s' % file_name
    output = subprocess.call(['javac', file_name])

请注意,该脚本将在当前目录中创建一个类名的文件。最好使用 /tmp 中的某些内容,但请记住它必须与类命名相同。如果您正在测试脚本,请不要执行以下操作:

cat MyImportantJava.java | python javac-stdin.py

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:

import fileinput, re, subprocess

class_name = None

buffer = []
class_matcher = re.compile('\w+ class (?P<name>\w+)')

for line in fileinput.input():
    if None == class_name:
        buffer.append(line)
        m = class_matcher.match(line)
        if m:
            class_name = m.group('name')
            print "found %s" % class_name
            file_name = '%s.java' % class_name
            out = open(file_name, 'wb')
            out.writelines(buffer)
    else:
        out.write(line)

if None == class_name:
    print "Error: no class found"

else:
    out.close()

    print 'javac %s' % file_name
    output = subprocess.call(['javac', file_name])

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:

cat MyImportantJava.java | python javac-stdin.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文