java - 运行单文件源代码 Java 程序时如何传递编译器选项?

标签 java javac java-11

我想使用 JEP 330使用 Java (>= 11) 运行单文件源代码程序。

这样做,我想传递编译器 (javac) 而不是运行时 (java) 理解的选项,例如-XDsuppressNotes。这导致例如以下调用失败:

java --enable-preview --source=12 -XDsuppressNotes Test.java

Unrecognized option: -XDsuppressNotes
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

在这种情况下,我如何指定此类特定于编译器的选项?

最佳答案

How can I specify such compiler-specific option in this case?

简短的回答:你不能。

这个 JEP 的目标不是取代 javac !只是为了更方便,尤其是在开始编程的情况下,达到“运行这个程序”的目的。


JEP-330与标准 javac -> java 链相比有一系列限制。 JEP 本身的引述:

As of JDK 10, the java launcher operates in three modes: launching a class file, launching the main class of a JAR file, or launching the main class of a module. Here we add a new, fourth mode: launching a class declared in a source file.

...

In source-file mode, the effect is as if the source file is compiled into memory, and the first class found in the source file is executed. For example, if a file called HelloWorld.java contains a class called hello.World, then the command

java HelloWorld.java

is informally equivalent to

javac -d <memory> HelloWorld.java

java -cp <memory> hello.World

Any arguments placed after the name of the source file in the original command line are passed to the compiled class when it is executed. For example, if a file called Factorial.java contains a class called Factorial to calculate the factorials of its arguments, then the command

java Factorial.java 3 4 5

is informally equivalent to

javac -d <memory> Factorial.java

java -cp <memory> Factorial 3 4 5

In source-file mode, any additional command-line options are processed as follows:

  • The launcher scans the options specified before the source file for any that are relevant in order to compile the source file. This includes: --class-path, --module-path, --add-exports, --add-modules, --limit-modules, --patch-module, --upgrade-module-path, and any variant forms of those options. It also includes the new --enable-preview option, described in JEP 12.

  • No provision is made to pass any additional options to the compiler, such as -processor or -Werror.

...


换句话说,在运行单文件源代码 Java 程序时应牢记某些限制:

  • 没有外部类,仅限单文件程序 - 除了您正在执行的文件外,您不得调用文件中的任何其他类。

  • 没有可用的类文件 - java 的调用以这种方式使用的工具不会产生任何 *.class您可以在工作目录中看到的文件。

  • 如果类路径中存在现有的类文件,您将被迫使用它 - 对于文件,例如 SampleClass.java ,应该是一个现有的类文件,比如说 SampleClass.class存在,您不能调用 java以源文件模式运行源文件的工具。

  • 文件名,不是类名 - java工具在执行源文件时考虑文件名而不是类名。

  • 文件中的第一个类,不匹配文件类名 - 类加载器不再通过匹配文件名和类名来确定要执行的类。文件中的第一个类是将要运行的类,

  • 源文件中的公共(public)文件没有限制 - javac关心源文件中公共(public)类的数量,java一点也不在乎。

  • 您不能传递某些特定于编译器的参数 - 类似 -Werror 的参数或 -nowarn你可以传递给javac , 可能不会被 java 通过(或认可)工具。

关于java - 运行单文件源代码 Java 程序时如何传递编译器选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797298/

相关文章:

JSON 解析错误 : Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists)

java - 如何在不将对象作为字段包含在内的情况下使项目中的所有类都可以使用该对象?

java - Akka 持久性 - 当收到消息传递确认时,从日志中删除消息(或标记为已确认)

将 Assets 复制到 sdcard 时出现 java.io.IOException(使用 assethelper)

java - javac 中有歧义的构造函数引用,但 Eclipse 中没有

java - 使用 Javassist 编译的类的类路径

java - 为什么 Java 字节码方法中使用的局部变量数量不是最经济的?

java - JDK 11 PreMaster Secret 调试

gradle - 如何在 Raspberry Pi 上运行 JavaFX 11 应用程序?

java - 关于Java中垃圾收集的问题