无法编译java 21

Dip*_*esh 2 java java-21

我在java 21中看到,你不需要在你的main方法中编写public static,你可以只写

public class Main{
    void main(String[] args){
        System.out.println("Hi");
    }
}
Run Code Online (Sandbox Code Playgroud)

我卸载了 java 17 并安装了 java 21 ,将其添加到环境变量中,但是当我运行此代码时,出现此错误。

Error: Main method not found in class Main, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application 
]
Run Code Online (Sandbox Code Playgroud)

当我在 cmd 中编写 java --version 时,我得到的 java 版本为 21 但代码仍然无法编译

rzw*_*oot 8

未命名类是一个预览功能,这意味着除非您选择加入,否则它不存在:它不适用于生产代码,并且可能会在将来的版本中中断,因此您必须选择加入:

javac --release 21 --enable-preview MyFile.java
java --enable-preview myFile
Run Code Online (Sandbox Code Playgroud)