Reputation: 11
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
Reputation: 10051
I've tried your code and it gave me a different error.
These are the steps I've tried:
Create a class TestingSwitch
vi TestingSwitch.java
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;
}
}
}
Compile the class
javac TestingSwitch.java
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
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
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
Reputation: 2861
Wrong method signature.
Change:
public static void main(String args)
To:
public static void main(String[] args)
Upvotes: 1
Reputation: 1252
Here is your error :
public static void main(String args)
You have to write
public static void main(String[] args)
Upvotes: 1