Showing posts with label classpath. Show all posts
Showing posts with label classpath. Show all posts

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?

Monday, 10 June 2019

Java: How to make the APIs available to all applications


Approach 1: Place the jar file in global classpath.

Approach 2: Place the jar file in ext directory of jre installation.

When java application starts, jre checks for the classes in core system classes (rt.jar file, which is located in jre lib), ext directory of jre and folders from classpath.

In MAC, you can find the ext directory location from here.
/Library/Java/JavaVirtualMachines/{JAVA_VERSION}/Contents/Home/jre/lib/ext

Previous                                                 Next                                                 Home

Exploring CLASSPATH


‘CLASSPATH’ tells java applications, where to look for java classes.

CLASSPATH variable
If you set the CLASSPATH variable to one or more paths, then Java classloader looks for the needed classes in these paths.

Let me explain with an example.
Create App.java file under the directory com/sample/app.

$tree
com
└── sample
    └── app
        └── App.java

2 directories, 1 file


Compile App.java.
$javac com/sample/app/App.java
$
$tree com
com
└── sample
    └── app
        ├── App.class
        └── App.java

2 directories, 2 files

As you see, App.class file is created in com/sample/app folder.


Try to run App file.
$java com/sample/app/App
Error: Could not find or load main class com.sample.app.App

We got this error, since Java class loader do not know, from where to load the classes. You can solve this problem by setting CLASSPATH variable to the folder where 'com' folder is located.

For example
export CLASSPATH=/Users/krishna/Documents/TechnicalDocuments

If you are using Windows system, use below statement

SET CLASSPATH={path_to_com_folder}
$tree 
com
└── sample
    └── app
        ├── App.class
        └── App.java

2 directories, 2 files
$
$pwd
/Users/krishna/Documents/TechnicalDocuments
$
$export CLASSPATH=/Users/krishna/Documents/TechnicalDocuments
$
$java com/sample/app/App
Hello World

Can I set multiple folder paths to classpath?
Yes you can set multiple folders to classpath by separating them by : in linux, ; in Windows.

Example
export CLASSPATH=/Users/krishna/Documents/tutorial;/Users/krishna/Documents/examples;


Set the classpath at the application level

Using -cp option, you can specify the classpath.
$java -cp /Users/krishna/Documents/TechnicalDocuments com.sample.app.App
Hello World
$
$java -cp /Users/krishna/Documents/TechnicalDocuments com.sample.app/App
Hello World
$
$java -cp /Users/krishna/Documents/TechnicalDocuments com.sample/app/App
Hello World
$
$java -cp /Users/krishna/Documents/TechnicalDocuments com/sample/app/App
Hello World

When you set the variable CLASSPATH, it is available for all the applications. All the application look for the folders set in CLASSPATH variable to load the classses. Some cases it may lead to chaos, for example, in one application you are using gson library 1.1, but in other you are using gson library 2.2. When the classloader tries to load gson classes, it will load whichever it enocounter in the path first. So it is always better to use application specific classpath.

Reference



Previous                                                 Next                                                 Home