最近,我安装了Java 11.编译时,不小心,而不是使用javac Main.java编译Main.java,我编写了java Main.java.它没有向我显示任何错误,并且没有任何问题.为什么会这样?这是新版Java中的新功能吗?
Main.java的内容:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
在Java 8中运行:
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
java Main.java
Error: Could not find or load main class Main.java
在Java 11中运行:
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
java Main.java
Hello, World!
解决方法:
此功能是作为JEP 330的一部分引入的:启动单文件源代码程序.
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 HelloWorld.java java -cp hello.World
标签:java
来源: https://codeday.me/bug/20190527/1161440.html