Balaji.K.S
Balaji.K.S

Reputation: 11

Exception in thread "main" java.lang.NoClassDefFoundError instead of MainMethodNotFound

On executing the below class, "Exception in thread 'main' java.lang.NoClassDefFoundError" is thrown. I expected the exception to be thrown as "MainMethodNotFoundException".

Why noClassDefFoundError was thrown here?

public class TestingSwitch {
    public static void main(String args) {
        int cnt = 1;
        switch(cnt){
            default: 
                System.out.println("Welcome");
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
        }
    }

}

Upvotes: 1

Views: 326

Answers (4)

jfcorugedo
jfcorugedo

Reputation: 10051

I've tried your code and it gave me a different error.

These are the steps I've tried:

  1. Create a class TestingSwitch

    vi TestingSwitch.java
    
  2. Put there your content

    $ cat TestingSwitch.java 
    public class TestingSwitch {
       public static void main(String args) {
          int cnt = 1;
          switch(cnt){
             default: 
                System.out.println("Welcome");
             case 1:
                System.out.println("One");
                break;
             case 2:
                System.out.println("Two");
                break;
          }
       }
    }
    
  3. Compile the class

    javac TestingSwitch.java 
    
  4. Now there are two files in the directory

    -rw-r--r--   1 xxxx  xxxx   518 Oct 26 11:33 TestingSwitch.class
    -rw-r--r--   1 xxxx  xxxx   373 Oct 26 11:33 TestingSwitch.java
    
  5. Try to execute the class:

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

I'm using Oracle JDK 1.8:

  java version "1.8.0_31"
  Java(TM) SE Runtime Environment (build 1.8.0_31-b13)

Upvotes: 0

soorapadman
soorapadman

Reputation: 4509

You have to change public static void main(String args) this to public static void main(String[] args)

run this

public class Test {

        public static void main(String[] args) {
            int cnt = 1;
            switch(cnt){
                default:
                    System.out.println("Welcome");
                case 1:
                    System.out.println("One");
                    break;
                case 2:
                    System.out.println("Two");
                    break;
            }
        }

}

Upvotes: 1

sinclair
sinclair

Reputation: 2861

Wrong method signature.

Change:

public static void main(String args) 

To:

public static void main(String[] args)

Upvotes: 1

Aroniaina
Aroniaina

Reputation: 1252

Here is your error :

public static void main(String args) 

You have to write

public static void main(String[] args)

Upvotes: 1

Related Questions