Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to compile java 21

Tags:

java

java-21

I saw in java 21 , you don't need to write public static in you main method and you can just write

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

I uninstalled java 17 and installed java 21 , added it to the environment variables but when I am running this code , I am getting this error.

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 
]

When I am writing java --version in cmd , I am getting the version of my java as 21 but still the code is not compiling

like image 385
Dipesh Avatar asked Aug 02 '26 14:08

Dipesh


1 Answers

Unnamed classes is a preview feature, which means it doesn't exist unless you opt in: It's not meant for production code and is likely to break in a future release, hence why you must opt in:

javac --release 21 --enable-preview MyFile.java
java --enable-preview myFile
like image 114
rzwitserloot Avatar answered Aug 04 '26 02:08

rzwitserloot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!