java.lang.NOSuchMethodError: main Exception in thread “main”

There are lot of errors we have to face while programming. today we are going to know about this error mentioned below and also we will come to know that how to solve this kind of error

Reason behind the java.lang.NOSuchMethodError: main Exception in thread “main”:

Most of the new java programmers are known to this error message . many of them asked me actually what are the reasons behind this error and how can they resolve it.

So before going to resolve it we would suggest you to understand the reason behind java.lang.NoSuchMethodError: main Exception in thread “main” this error. If you understand the reason I hope you could resolve it yourself.

We use the command “java” to run a .java file . When you use the java command the command loads the class you mentioned in your program and then start searching for the main method or in easy way we can say that the compiler start looking for a method that usually looks like this :

public class anything {
...
public static void main (String[] args) {
   //body of main method ......
    }
}

So the main reason of this error is when you tried to compile your program but the code starts to call a method but the method is not available there or not existed on the class and the method might be static or non-static that does not matter at all.
On the above program the entry method is the main method which must have some minimum requirements . I would like to explain the requirements .
#1. The method must be in a class which is nominated before . (here the class is “anything”)

#2. We must have to make the main method public.

#3. We must have to make the main method static too .

#4. Return value must be a null value that means we have to make the return type void .

#5. We cant use more than one argument in this method . we need exactly one argument and the argument type must be String[] type

 

if any of the above requirements is not satisfied then we will find this error
java.lang.NoSuchMethodError: main Exception in thread “main”

or we can say this there are two main reason
1. The class which we are trying to run , there is no main method
2. there is incorrect signature of the main method .
lets see one example to understand it in a better way .

public class anything {
...
public static void main (String  args) {
   //body of main method ......
    }
}

now look at the code carefully we did not put [] this  . if we compile the code. we will get this error message

java.lang.NoSuchMethodError: main Exception in thread “main”

7 responses to “java.lang.NOSuchMethodError: main Exception in thread “main””

  1. bala says:

    hi,

    Exception in thread “main” java.lang.NoSuchMethodError: ‘void com.google.common.base.Preconditions.checkState(boolean, java.lang.String, java.lang.Object, java.lang.Object, java.lang.Object)’
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
    at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123)
    at sampleCodes.gLanch.main(gLanch.java:11)

    Iam getting this error while running the basic code in eclips.

  2. Anshu Shaw says:

    The error you’re seeing suggests a version conflict with the Guava library used by Selenium. To resolve it:

    1.Ensure compatible versions of Selenium WebDriver and WebDriver executable (e.g., ChromeDriver).
    2.Update Selenium and WebDriver to the latest versions.
    3.Check and manage dependencies, avoiding conflicting versions in your build tool (e.g., Maven or Gradle).
    4.Confirm there are no duplicate libraries in your project’s classpath.
    5.Clean and rebuild your project.
    6.Consider using dependency locking to maintain version consistency.
    7.Investigate transitive dependencies for Guava conflicts.
    8.Avoid system-level libraries that may interfere with project dependencies.
    Following these steps should help resolve the issue.

  3. Usha sahu says:

    It looks like there’s a syntax error in the code you provided. The error is likely because you have a typo in your code. The keyword “fiend” is not a valid keyword in Java. I assume you meant to write “find” or something else.

    Here’s the corrected code:

    “`java
    public class Anything {
    public static void main(String[] args) {
    // Body of main method ……
    }
    }
    “`

    I made the following corrections:
    1. Changed the class name to start with a capital letter (Java naming conventions recommend using CamelCase for class names).
    2. Added square brackets `[]` after `String` in the `main` method parameter list to properly define an array of String arguments.
    3. Removed the “fiend” error and replaced it with “find” for clarification, but you should replace it with the appropriate code you intended to write in the main method.

    Make sure to replace the comment `// Body of main method ……` with your actual code logic for the `main` method.

  4. SanjitKumar says:

    The “java.lang.NoSuchMethodError: main” error occurs when you try to run a Java class, but the Java Virtual Machine (JVM) cannot find the `public static void main(String[] args)` method in your class. This method is the entry point for running a Java program, and it must be declared exactly as shown.

    Here are some others common reasons why you might encounter this error: already others error reasone is there so i am contribute another error that is existe

    1. Incorrect class name: Ensure that the name of your Java class matches the filename (including capitalization). For example, if your class is named `MyClass`, the filename should be `MyClass.java`.

    2. Classpath issues: If you are using external libraries or dependencies, there might be a version conflict or classpath issue causing this error. Make sure you are using the correct versions of libraries and that they are included in the classpath correctly.

    3. Running the wrong class: Double-check that you are trying to run the correct class containing the `main` method. If you have multiple classes, ensure that you specify the correct class name when running your program.

    If you’ve confirmed that your code has a properly defined `main` method and there are no compilation errors, but you still encounter this error, there might be a more specific issue related to your project setup or environment.

  5. Rakesh Kumar Mandal says:

    If you execute a class file that does not have main method or you mistype the main method (e.g., by typing Main instead of main ), a No Such Method Error will occur.

  6. Rohan Raj Gupta says:

    The error message you’re seeing, “illegal start of type,” often indicates a problem in your Java code structure. In your provided code snippet, it seems you have an ellipsis (…) where the actual code should be in your class. The ellipsis is a placeholder for code, but it should be replaced with valid Java code.

    Here’s an example of how your code should look with a basic structure:

    “`java
    public class Anything {
    public static void main(String[] args) {
    // Your code goes here
    }
    }
    “`

    Make sure to replace “// Your code goes here” with the actual Java code you want to execute. Also, note that the class name should start with an uppercase letter, as per Java naming conventions.

  7. Prabhat Kumar says:

    No Such Method Error
    If we execute a class file that does not have a main method or we mistype the main method .
    For eg :- (by typing Main instead of main),a no such method error will occur

Leave a Reply

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