Mac Terminal: Could not find or load main class CLASSNAME

Start by making sure you are at the directory above the top level package

If the class belongs to the package com.foo.bar, you want to be in the directory above com.

In your case, you want to be in the directory above startPack.

Then you need to use the fully qualified name to run the class...

java statPack.mainClass

For example...


Make sure you have the current directory inside your CLASSPATH.

java -cp . mainClass

To set this globally, you can use export CLASSPATH=$CLASSPATH:. inside .bash_profile.

Separately, if your class lives inside a package such as com.foo.bar, then you will need to go to the parent directory of com and run your application with the full path.

java com.foo.bar.mainClass

I too faced this on Mac machine and then what I had to do to make it work was:

Problem Statement:

I had one package xyz under the root of project i.e src/main/java and then inside xyz package I had one class Student.java

my current directory is /Users/username/projectname/src/main/java/xyz: I can see Student.java exists here and I compiled it using javac Student.java

Now I see class file has been created at this location. But when I try to run the class file using java Student I get the error: Error: Could not find or load main class Student

Solution:

Now the solution is to go one step back in the directory and go to root path:/Users/username/projectname/src/main/java and run the command

java xyz.Student

and it will work.

Link to follow: https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html