$ java --version
openjdk 23 2024-09-17
OpenJDK Runtime Environment (build 23+37-2369)
OpenJDK 64-Bit Server VM (build 23+37-2369, mixed mode, sharing)
$ cat Hello.java
main() {
println("Hello");
}
$ java --enable-preview Hello.java
error: no class declared in source file
$ cat Hello2.java
void main() {
println("Hello");
}
$ java --enable-preview Hello2.java
Hello
$
It’s true that Hello.java does not declare a class, since the void modifier is missing from the instance main method. The error message, however, gives no hint of that reason. It would be far more helpful if it said something like “void modifier required in declaration of instance main method”.
openjdk 23 2024-09-17
OpenJDK Runtime Environment (build 23+37-2369)
OpenJDK 64-Bit Server VM (build 23+37-2369, mixed mode, sharing)
$ cat Hello.java
main() {
println("Hello");
}
$ java --enable-preview Hello.java
error: no class declared in source file
$ cat Hello2.java
void main() {
println("Hello");
}
$ java --enable-preview Hello2.java
Hello
$
It’s true that Hello.java does not declare a class, since the void modifier is missing from the instance main method. The error message, however, gives no hint of that reason. It would be far more helpful if it said something like “void modifier required in declaration of instance main method”.
- duplicates
-
JDK-8338301 Error recovery and reporting should be improved for erroneous implicitly declared classes
- Resolved