Java 11在使用’java’编译类时不会生成.class文件。

huangapple go评论66阅读模式
英文:

Java 11 does not produce a .class file when compiling the class with 'java'

问题

我安装了Java 11。当我运行命令 java <filename.java> 时,用于编译和运行代码,它会编译并运行程序,在CMD中显示输出,但不会生成任何 .class 文件。

你能解释一下为什么它不会生成 .class 文件吗?

英文:

I have Java 11 installed. When I run the command java &lt;filename.java&gt;, for compiling and running the code, it compiles and runs the program, shows the output in CMD, but it doesn't produce any .class file.

Could you explain to me why it does not produce the .class file?

答案1

得分: 13

请参阅JEP 330

自从Java 11版本以来,java FileName.java命令编译并运行FileName.java文件;然而,编译过程在“幕后”进行,不会显式生成对应的.class文件。相反,它会直接将相应的字节码加载到JVM实例中。

动机

> 单文件程序(整个程序适合放在单个源文件中)- 在学习Java的早期阶段以及编写小型实用程序时很常见。在这种情况下,编译程序在运行之前是纯粹的形式主义。此外,单个源文件可能会编译成多个类文件,这为实现“运行此程序”的简单目标增加了打包开销。希望能够通过java启动器直接从源代码运行程序:

java HelloWorld.java

如果您希望将.class文件作为输出,仍然应该使用javac,如下所示:

javac FileName.java
英文:

Have a look at JEP 330.

Since Java 11, java FileName.java compiles and runs FileName.java; however, compilation happens "behind the scenes", without explicitly producing corresponding .class file. Instead, it directly loads corresponding byte-code into the JVM instance.

Motivation

>Single-file programs (where the whole program fits in a single source file) - are common in the early stages of learning Java, and when writing small utility programs. In this context, it is pure ceremony to have to compile the program before running it. In addition, a single source file may compile to multiple class files, which adds packaging overhead to the simple goal of "run this program". It is desirable to be able to run the program directly from source with the java launcher:

java HelloWorld.java

If you want to have the .class file as an output, you should still use javac, as:

javac FileName.java

huangapple
  • 本文由 发表于 2020年9月23日 01:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64014921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定