How to resolve "Could not find or load main class package" in Java?

JavaObject Oriented ProgrammingProgramming

Once you write a Java program you need to compile it using the javac command, this shows you the compile time errors occurred (if any).

Once you resolve them and compile your program success fully, an executable file with the same name as your class name is generated in your current folder, with the .class extension.

Then you need to execute it using the java command as −

java class_name

While executing, when JVM does not find a .class file with the specified name then a run time error occurs saying “Could not found or load main class” error as −

D:\sample>java Example
Error: Could not find or load main class Example
Caused by: java.lang.ClassNotFoundException: Example

Solution

To avoid this error, you need to specify the absolute (including packages) name of the .class file (just name) which is in the current directory.

Following are the scenarios where this error could occur −

Wrong class name − You might have specified the wrong class name.

class Example {
public static void main(String args[]){
System.out.println("This is an example class");
}
}

Error

D:\>javac Example.java
D:\>java Exmple
Error: Could not find or load main class Exmple
Caused by: java.lang.ClassNotFoundException: Exmple

Solution − In this the class name is wrongly spelt, we need to correct it.

D:\>javac Example.java
D:\>java Example
This is an example class

Wrong case − You need to specify the name of the class with same case Example.java is different from example.java.

class Example {
public static void main(String args[]){
System.out.println("This is an example class");
}
}

Error

D:\>java EXAMPLE
Error: Could not find or load main class EXAMPLE
Caused by: java.lang.NoClassDefFoundError: Example (wrong name: EXAMPLE)

Solution − In this the class name is with wrong case it, it should be decorated.

D:\>javac Example.java
D:\>java Example
This is an example class

Wrong package − You might have created the .class file in a package and tried to execute without package name or with wrong package name.

package sample;
class Example {
public static void main(String args[]){
System.out.println("This is an example class");
}
}

Error

D:\>javac -d . Example.java
D:\>java samp.Example
Error: Could not find or load main class samp.Example
Caused by: java.lang.ClassNotFoundException: samp.Example

Solution − In this scenario we have mention the name of the wrong package While executing we need to specify the correct package name in which the .class file exists as −

D:\>javac -d . Example.java
D:\>java sample.Example
This is an example class

Inclusion of .class extension − While executing a file there is no need to include the .class extension in your program you just need to specify the name of the class file.

Error

D:\sample>java Example.class
Error: Could not find or load main class Example.class
Caused by: java.lang.ClassNotFoundException: Example.class

Solution − The extension .class is not required while executing the program.

D:\sample>java Example
This is an example class
Published on 14-Oct-2019 11:27:51