Wednesday 18 May 2022

How to solve the Error: Could not find or load main?

'Error: Could not find or load main class HelloWorld' error occur when you try to run the main class HelloWorld, but Java runtime unable to find it.

 

Let’s try to produce the error and see the possible options to resolve it.

 

Step 1: Create a folder hierarchy com/sample/app and define HelloWorld.java file in it.

 

HelloWorld.java

package com.sample.app;

public class HelloWorld {
	
	public static void main(String[] args) {
		System.out.println("Hello World");
	}

}

Folder hierarchy looks like below.


 

$tree
.
|____com
| |____sample
| | |____app
| | | |____HelloWorld.java

Step 2: Navigate to the folder, where package com is create and compile HelloWorld.java file by executing below command.

javac com/sample/app/HelloWorld.java


bash-3.2$ tree
.
|____com
| |____sample
| | |____app
| | | |____HelloWorld.java
bash-3.2$ 
bash-3.2$ javac com/sample/app/HelloWorld.java 
bash-3.2$ 
bash-3.2$ tree
.
|____com
| |____sample
| | |____app
| | | |____HelloWorld.class
| | | |____HelloWorld.java


Step 3: Run HelloWorld class file, by executing the command 'java com/sample/app/HelloWorld'.

 

bash-3.2$ java com/sample/app/HelloWorld
Hello World

Since, Java look into the current directory for the main class, program ran successfully. 

 How about running HelloWorld class from different directory?

Let's navigate to the root folder and try to run HelloWorld class.


bash-3.2$ cd /
bash-3.2$ 
bash-3.2$ java com/sample/app/HelloWorld
Error: Could not find or load main class com.sample.app.HelloWorld

We got this error, since Java class loader unable to find the main class HelloWorld in the current directory.

 

How to resolve this error?

Approach 1: Set the classpath using CLASSPATH variable. You can read this post for more details.

export CLASSPATH={path_to_the_parent_of_package_folder}

 

If you are using Windows system, use below statement

 

SET CLASSPATH={path_to_the_parent_of_package_folder}


In my case, I defined com/sample/app package hierarchy in /Users/krishna/Documents/technical-documents/examples folder.


export CLASSPATH=/Users/krishna/Documents/technical-documents/examples


Once you set the classpath, you can run the HelloWorld class from anywhere.


$export CLASSPATH=/Users/krishna/Documents/technical-documents/examples
$
$cd /
$
$java com/sample/app/HelloWorld
Hello World

You can even run using . syntax.

$java com.sample.app.HelloWorld
Hello World

Approach 2: Set the class path using -cp or -classpath option.

$java -cp /Users/krishna/Documents/technical-documents/examples com.sample.app.HelloWorld
Hello World
$
$java -classpath /Users/krishna/Documents/technical-documents/examples com.sample.app.HelloWorld
Hello World

Note

In case if you are using maven as a build tool for your project, it generate the class files under project_directory\target\classes folder.

 

 

 You may like

Interview Questions

Different ways to iterate over a map using for-each loop

How for-each or enhanced for loop works in Java?

How to solve UnsupportedClassVersionError in Java?

How to find the Java major and minor versions from a .class file

Can I run a class file that is compiled in lower environment?

No comments:

Post a Comment