gpt4 book ai didi

java - 在不编译的情况下运行 Java 程序

转载 作者:搜寻专家 更新时间:2023-10-31 19:51:47 28 4
gpt4 key购买 nike

最近装了Java 11,写代码的时候不小心把Main.java编译成javac 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

这是 JEP

关于java - 在不编译的情况下运行 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493058/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com