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

The java error: main method not found in the file, please define the main method as: public static void main(string[] args) occurs if the main method not found in class or the main method is not accessible or the main method is invalid. The main method indicates the start of the application. The main method syntax must be as public static void main(String[] args) { }. Alternatively, java program must extend javafx.application.Application. Otherwise, “or a JavaFX application class must extend javafx.application.Application” error will be shown.

In Java, each application must have the main method. The main method is the entry point of every java program. The java program searches for the main method while running. This error occurred when the main method is not available.

In java, the JavaFX application does not to have a main method, instead the JavaFX application must extend javafx.application.Application class. Otherwise “Error: Main method not found in class, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application” exception will be thrown in java.



Exception

Error: Main method not found in class, please define the main method as:
    public static void main(String[] args)
 or a JavaFX application class must extend javafx.application.Application
Error: Main method is not static in class, please define the main method as:
   public static void main(String[] args)
Error: Main method must return a value of type void in class, please 
define the main method as:
   public static void main(String[] args)


Error Code

The Error code looks like as below. If you run javac Test.java & java Test The above exception will be thrown. The Test class neither have main method nor extend javafx.application.Application class.

package com.yawintutor;

public class Test {

}


Root Cause

Spring boot is looking for the main method to start the spring boot application. This error occurred because the main method is not available.

Any one of the following is the possible cause of this issue

  • The main method is deleted
  • The main method name is renamed
  • The signature of the main method is changed
public static void main(String[] args) {
// block of code 		
}


Solution 1

The java main method is the entry point of the java program. If the main method is missing in the program, the exception will be thrown. Check the main method in the code, and add the main method as shown below.

package com.yawintutor;

public class Test {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

The below example shows the main method of the spring boot application.

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringHelloWorldApplication {

	public  void main(String[] args) {
		SpringApplication.run(SpringHelloWorldApplication.class, args);
	}

}


Solution 2

The main method in the java application must be public. If it is not a public, java compiler can not access the main method. If the access specifier is not specified or not a public, then the exception will be thrown.

package com.yawintutor;

public class Test {
	static void main(String[] args) {
		System.out.println("Hello World");
	}
}

Output

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

Solution

package com.yawintutor;

public class Test {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}


Solution 3

If the main method does not have static keyword, the main method can not access from outside of the class. The object of the class only can access the main method. To create a object, the main method must be executed. This will cause the exception.

package com.yawintutor;

public class Test {
	public void main(String[] args) {
		System.out.println("Hello World");
	}
}

Output

Error: Main method is not static in class com.yawintutor.Test, please define the main method as:
   public static void main(String[] args)

Solution

package com.yawintutor;

public class Test {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}


Solution 4

The main method must return void. If the return data type is added other than void. the exception will be thrown.

package com.yawintutor;

public class Test {
	public static int main(String[] args) {
		System.out.println("Hello World");
		return 0;
	}
}

Output

Error: Main method must return a value of type void in class com.yawintutor.Test, please 
define the main method as:
   public static void main(String[] args)

Solution

package com.yawintutor;

public class Test {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}


Solution 5

The main method must have only one argument. The argument must be a string array. If no argument is configured or different data type argument is configured or more than one argument is configured, the exception will be thrown.

package com.yawintutor;

public class Test {
	public static void main() {
		System.out.println("Hello World");
	}
}

Output

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

Solution

package com.yawintutor;

public class Test {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

How to create simple Spring Boot Application with Main method



10 Comments

  • Coder Tony , March 24, 2022 @ 2:04 PM

    To solve this we have to create a new project in different location and try.
    If still problem persist that means your JDK may have some issue, So you need to reinstall eclipse and try.

  • Moksh , June 13, 2021 @ 11:39 AM

    Vijay , I too get the same error even though I declare main method . If you get solution for this error , please reply me how did you solve it ? Plssss plss

    • Yawin Tutor , June 25, 2021 @ 7:38 PM

      Hi Moksh, Please share your code.

      • P. Mokshitha , July 5, 2021 @ 10:04 AM

        Yeah ok
        class Hello
        {
        public static void main (String args[])
        {
        System. Out. Println(“Hello Java”);
        }
        }
        Eventhough I type this simple program it was throwing me a error like main method not found in class Hello, please define main method as: public static void main (String args[]) or a javaFx application class must extend javaFx. application. Application
        Please help it was troubling me from last 1 month

        • Yawin Tutor , July 6, 2021 @ 11:45 AM

          Try the following options

          1. I assume you are using eclipse. Create a new project in different location and try.
          2. JDK may have some issue in your eclipse, reinstall eclipse and try
          3. run your code in command prompt and check
          javac -version
          java -version
          javac Hello.java
          java Hello

        • ahamedibrahim , January 22, 2022 @ 9:12 AM

          make class as public #

        • hasti hajipara , March 9, 2022 @ 7:18 AM

          class Hello
          {
          public static void main (String args[])
          {
          System.out.println(“Hello Java”);
          }
          }
          in your program syntax error is there
          and in my laptop , i not found eny error and it is working

      • smita , June 26, 2023 @ 12:48 PM

        package test;

        import java.io.Closeable;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.sql.Driver;
        import java.util.Iterator;
        import java.util.Set;

        import org.apache.poi.hssf.usermodel.HSSFCell;
        import org.apache.poi.hssf.usermodel.HSSFSheet;
        import org.apache.poi.hssf.usermodel.HSSFWorkbook;
        import org.apache.poi.sl.usermodel.Sheet;
        import org.apache.poi.ss.usermodel.Row;
        import org.apache.poi.ss.usermodel.Workbook;
        import org.apache.poi.ss.usermodel.WorkbookFactory;
        import org.apache.poi.xssf.usermodel.XSSFCell;
        import org.apache.poi.xssf.usermodel.XSSFRow;
        import org.apache.poi.xssf.usermodel.XSSFSheet;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
        import org.apache.xmlbeans.impl.xb.xsdschema.Public;
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.chrome.ChromeDriver;
        import org.openqa.selenium.interactions.Actions;
        import org.openqa.selenium.support.ui.Select;
        import com.google.common.collect.Table.Cell;

        public class Basic {
        @SuppressWarnings(“rawtypes”)
        public static void main(String[] args, String Value) throws IOException {
        System.setProperty(“webdriver.chrome.driver”,”C:\\selenium webdriver\\chromedriver\\chromeDriver\\chromeDriver\\chromedriver.exe”);
        WebDriver driver = new ChromeDriver();
        driver.get(“https://lms-testing-vapt-1683764238.ap-south-1.elb.amazonaws.com/OmniFin/loadLMS.do”);
        driver.manage().window().maximize();
        try {
        Thread.sleep(5000);
        } catch (InterruptedException e) {

        e.printStackTrace();
        }
        driver.findElement(By.xpath(“//button[@id=’details-button’]”)).click();
        driver.findElement(By.xpath(“//a[@id=’proceed-link’]”)).click();
        driver.findElement(By.xpath(“//input[@id=’userName’]”)).sendKeys(“BG430235”);
        driver.findElement(By.xpath(“//input[@id=’userPassword’]”)).sendKeys(“admin@a3s”);
        driver.findElement(By.xpath(“//input[@id=’captchaCode’]”)).click();
        try {
        Thread.sleep(10000);
        } catch (InterruptedException e) {

        e.printStackTrace();
        }
        driver.findElement(By.xpath(“//input[@name=’loginbutton’]”)).click();
        driver.switchTo().alert().accept();
        try {
        Thread.sleep(5000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        driver.findElement(By.xpath(“//a[normalize-space()=’CREDIT MANAGEMENT’]”)).click();
        driver.switchTo().frame(1);
        driver.findElement(By.xpath(“//a[contains(text(),’CUSTOMER SERVICE’)]”)).click();
        try {
        Thread.sleep(5000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        driver.findElement(By.xpath(“//a[@id=’hrefIda15b0′]”)).click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame(2);

        driver.findElement(By.xpath(“//input[@id=’loanAccountButton’]”)).click();
        Set windows = driver.getWindowHandles();
        Iterator it = windows.iterator();
        String parentId = (String) it.next();
        String childId = (String) it.next();
        driver.switchTo().window(childId);
        System.out.println(“Child window title “+ driver.getTitle());

        FileInputStream inputStream = new FileInputStream(“C:\\selenium webdriver\\Data.xlsx”);
        XSSFWorkbook wb=new XSSFWorkbook(inputStream);
        XSSFSheet sheet1 = wb.getSheetAt(0);
        String data0 = sheet1.getRow(0).getCell(0).getStringCellValue();
        System.out.println(data0);
        driver.findElement(By.xpath(“//input[@id=’lovCode’]”)).sendKeys(data0);
        try {
        Thread.sleep(500);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        driver.findElement(By.xpath(“//input[@class=’searchLovButton’]”)).click();
        driver.findElement(By.xpath(“//input[@id=’selectRadioBtn’]”)).click();
        driver.switchTo().window(parentId);
        System.out.println(“Parent window title: “+ driver.getTitle());
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        driver.switchTo().defaultContent();
        driver.switchTo().frame(2);
        driver.findElement(By.xpath(“//button[@id=’search’]”)).click();
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        driver.findElement(By.xpath(“//button[@id=’loan_viewer’]”)).click();

        // String dealValue = dealNum.getAttribute(“value”);
        // System.out.println(dealValue);
        WebElement dealNum = driver.findElement(By.xpath(“//input[@id=’dealNo’]”));
        System.out.println(dealNum);
        //String s=dealNum.getText();
        //System.out.println(s);

        System.out.println(“Hello”);
        //driver.close();

        }
        }

        plz reply same error for main method

    • ankush , June 11, 2023 @ 11:10 PM

      how did solve is

  • Digvijay , October 4, 2020 @ 12:55 PM

    package exercise;

    import java.util.Scanner;

    public class PrimeNumbers {

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println(“Enter the Range : “);

    int a = sc.nextInt();
    int b = sc.nextInt();

    System.out.print(a + ” ” + b);

    for (int i = a ; i <= b ; i++)
    {
    for (int j = 2 ; j < i ; j++)
    {
    if (i % j == 0)
    {
    break;

    }
    }

    System.out.print("Prime Numbers : " );
    }

    }

    }

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

    what to do in it

Leave a Reply

Your email address will not be published. Required fields are marked *