0
votes

Hi I'm new to java and facing the below error

i'm trying to run this script

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo { 
    public static void main(String [] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\javacoding\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://google.com");
    }
}

But get this error when i try to run it

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

1
The code should work fine, are you sure you're using the code from teh snippet provided? - AK47
or a JavaFX application class must extend javafx.application.Application maybe? something like public class Demo extends javafx.application.Application ? (or just Application, but be sure you will import correct file) - xxxvodnikxxx
What is your package structure ? - Alpesh Jikadra

1 Answers

0
votes

Regarding the docs with example and as error message says

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

The main is in place, but inheritance not, you will need to extend javafx.application.Application, so assuming that should fix the issue:

replace the class declaration as public class Demo to public class Demo extends javafx.application.Application

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo extends javafx.application.Application{ 
    public static void main(String [] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\javacoding\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://google.com");
    }
}

Or just public class Demo to public class Demo extends Application with import javafx.application.Application; (in the import section of the class)

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import javafx.application.Application; //added 


public class Demo extends Application{ 
    public static void main(String [] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\javacoding\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://google.com");
    }
}

It seems you did not created the correct type of the project in your IDE, maybe re-creation will be needed (like eg. in case of maven project you need some additional files like pom.xml, etc.)