Fix Java's 'Could Not Find or Load Main Class' Jar error |Video upload date:  · Duration: PT4M14S  · Language: EN

Quick guide to diagnose and fix the Could Not Find or Load Main Class error when running a jar file on the command line

Why the JVM yelled at you

You double clicked your JAR or typed java -jar myapp.jar and the JVM answered with Could not find or load main class. That is the Java way of saying it tried to start your program and failed because something was not where it expected it to be. No drama, just detective work.

Quick checklist to stop the blaming

  • Confirm the manifest declares the main class
  • Make sure the main method signature is exactly right
  • Verify package names match folder layout inside the JAR
  • Ensure .class files are actually in the archive
  • Run the correct java command for your packaging style
  • Check Java version and conflicting classes on the classpath

Check the JAR manifest

Start by listing the archive. Run jar tf myapp.jar and scan for META-INF/MANIFEST.MF and the compiled classes. To inspect the manifest itself use jar xf myapp.jar META-INF/MANIFEST.MF and then open META-INF/MANIFEST.MF with your favorite editor. The manifest should include a Main Class entry that names the fully qualified main class for example com.example.Main, not something half baked.

Common manifest blunders

  • No Main Class entry at all, because the build did not mark the artifact as executable
  • A typo in the class name or wrong package
  • Manifest line split over multiple lines because of bad wrapping when creating the archive

Confirm the main method

The JVM will only launch a program that has public static void main(String[] args). Yes the signature is tyrannical and exact. If your main is missing, private, or has a different parameter list the JVM will refuse to cooperate.

Verify package layout inside the JAR

Java expects package com.example.app to be stored as com/example/app inside the JAR. Use jar tf myapp.jar to look for com/example/app/Main.class. If the folders do not match the package declaration, the class will not be found even if it compiled.

Inspect the JAR for compiled classes

Sometimes build systems forget to include compiled classes, or your build produced an empty jar for reasons you will regret later. Look for .class files in the expected locations. If they are missing, fix your build script or the IDE packaging step.

Run the application the right way

If the JAR is executable use java -jar myapp.jar. If you are launching from classpath use java -cp myapp.jar com.example.app.Main and provide the fully qualified class name. Do not mix styles or expect the JVM to guess what you meant.

Watch out for Java version and duplicate classes

Runtime version mismatches can prevent classes from loading. If your classes were compiled for a newer Java than the runtime you will see errors. Also avoid duplicate classes on the classpath. If two jars contain the same class, the JVM may pick the wrong one or fail to load the expected main class.

Quick troubleshooting steps

  1. Run jar tf myapp.jar and confirm META-INF/MANIFEST.MF exists
  2. Extract and open the manifest to verify the Main Class entry
  3. Look for the expected .class files and folder layout
  4. Confirm public static void main(String[] args) is present
  5. Launch with java -jar or java -cp as appropriate
  6. Check runtime Java version and remove conflicting jars

Recap with a tiny dollop of hope

Most of the time this error is a packaging or manifest problem, not a mystical curse. Verify the manifest, confirm the main method and package layout, and run the right command. If all else fails, blame the build script and then fix it.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.