Error: Could not find or load main classThe question about Error: Could not find or load main class 

Error: Could not find or load main class22 avaliable solutions to Error: Could not find or load main class 

You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;

Error: Could not find or load main class

If the class is in a package

package thepackagename;

public class TheClassName {
  public static final void main(String[] cmd_lineParams)  {
     System.out.println("Hello World!");
  } 
}

Then calling:

java -classpath . TheClassName

results in Error: Could not find or load main class TheClassName. This is because it must be called with its fully-qualified name:

java -classpath . thepackagename.TheClassName

And this thepackagename directory must exist in the classpath. In this example, ., meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which thepackagename exists.

To be clear, the name of this class is not TheClassName, It’s thepackagename.TheClassName. Attempting to execute TheClassName does not work, because no class having that name exists. Not on the current classpath anyway.

Finally, note that the compiled (.class) version is executed, not the source code (.java) version. Hence “CLASSPATH.”

Error: Could not find or load main class

You can try these two when you are getting the error: ‘could not find or load main class’

If your class file is saved in following directory with HelloWorld program name
d:/sample

  1. java -cp d:/sample HelloWorld
  2. java -cp . HelloWorld
Error: Could not find or load main class

I believe you need to add the current directory to the Java classpath

java -cp .:./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
Error: Could not find or load main class

You have to include classpath to your javac and java commands

javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main

suppose you have the following

Package Named: com.test
Class Name: Hello (Having main)
file is located inside “src/com/test/Hello.java”

from outside directory:

$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
  • In windows the same thing will be working too, I already tried
Error: Could not find or load main class

if You working in Eclipse, just make cleanup (project/clean.. clean all projects) of project.

Error: Could not find or load main class

You have to set the classpath if you get the error:

Could not find or load main class XYZ

For example:

E:/>set path="c:/programfiles/Java/jdk1.7.0_17/bin"
E:/>set classpath=%classpath%;.;
E:/>javac XYZ.java
E:/>java XYZ
Error: Could not find or load main class

I got this error because I was trying to run

javac HelloWorld.java && java HelloWorld.class

when I should have removed .class:

javac HelloWorld.java && java HelloWorld
Error: Could not find or load main class

If you try to run a java application which needs JDK 1.6 and you are trying to run on JDK 1.4 then you will come across this error. In general trying to run java appliation on old JRE may fail. Try installing new JRE/JDK.

Error: Could not find or load main class

I know this question was tagged with linux, but on windows, you might need to separate your cp args with a ; instead of a :.

java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar;./vensim.jar SpatialModel vars

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Error: Could not find or load main class

To check your BuildPath, it could be that you are referencing a Library which is not anymore existing.

Error: Could not find or load main class

If you’re getting this error and you are using Maven to build your Jars, then there is a good chance that you simply do not have your Java classes in src/main/java/.

In my case I created my project in Eclipse which defaults to src (rather than src/main/java/.

So I ended up with something like mypackage.morepackage.myclass and a directory structure looking like src/mypackage/morepackage/myclass, which inherently has nothing wrong. But when you run mvn clean install it will look for src/main/java/mypackage/morepackage/myclass.

It will not find the class but it won’t error either. So it will successfully build and you when you run your outputted Jar the result is:

Error: Could not find or load main class mypackage.morepackage.myclass

Because it simply never included your class in the packaged Jar.

Error: Could not find or load main class

Problem is not about your main function. Check out for

javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java     VensimHelper.java VensimException.java VensimContextRepository.java

output and run it.

Error: Could not find or load main class

java -verbose:class HelloWorld might help you understand which classes are being loaded.

Also, as mentioned before, remember to call the full qualified name (i.e. include package).

Error: Could not find or load main class

I was using Java 1.8, and this error suddenly occured when i pressed “Build and clean” in netbeans, I switched for a brief moment to 1.7 again, clicked Ok, re-opened properties and switched back to 1.8 and everything worked perfectly.

I hope i can help someone out with this, these errors can be quite time consuming.

Error: Could not find or load main class

This problem occurred for me when I imported an existing project into eclipse. What happens is it copied all the files not in the package, but outside the package. Hence, when I tried run > run configurations, it couldn’t find the main method because it was not in the package. All I did was copy the files into the package and Eclipse was then able to detect the main method. So ultimately make sure that Eclipse can find your main method, by making sure that your java files are in the right package.

Error: Could not find or load main class

If so simple than many people think, me included 🙂

cd to Project Folder/src/package there you should see yourClass.java then run javac yourClass.java which will create yourClass.class then cd out of the src folder and into the build folder there you can run java package.youClass

I am using the Terminal on Mac or you can accomplish the same task using Command Prompt on windows

Error: Could not find or load main class

If you are using Eclipse… I renamed my main class file and got that error. I went to “Run As” configurator and under the class path for that project, it had listed both files in the class path. I removed old class that I renamed and left the class that had the new name and it compiled and ran just fine.

Error: Could not find or load main class

This solved the issue for me today:

cd /path/to/project
cd build
rm -r classes

Then clean&build it and run the individual files you need.

Error: Could not find or load main class

I have a similar problem in Windows, it’s related to the classpath. From the command line, navigate until the directory where it’s located your Java file (*.java and *.class), then try again with your commands.

Error: Could not find or load main class

I use Anypoint Studio (an Eclipse based IDE). In my case everything worked well, until I found out that while running the java code, something totally different is executed. Then I have deleted the .class files. After this point I got the error message from this question’s title. Cleaning the project didn’t solve the problem.

After restarting the IDE everything worked well again.