• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

How to fix "ClassNotFoundException" error while running Java program

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning Java and trying to run a simple Java program using the command prompt. I created a basic program to print a message, and the program compiles successfully using the javac command. However, when I try to run the program using the java command, I get an error saying "Error: Could not find or load main class" or sometimes ClassNotFoundException.
Example code:
Java
Copy code
public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello World");
   }
}
Commands I used:
Copy code

javac HelloWorld.java
java HelloWorld
The program compiles without any errors, but when I try to run it, the system cannot find the main class.
I would like to know:
Why this error occurs even though the program compiles successfully.
Whether it is related to the classpath or folder location.
The correct way to compile and run a Java program from the command line.
Please explain the reason and provide the correct steps to resolve this issue.
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is how you would usually run a Hello World program. Please try one‑file execution:-You may be right about classpath problems. Please show us your classpath because that is a likely cause of your problem.
 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just tried it on my computer and it works fine with the command:Are you sure the file HelloWorld.class has been created?
 
Campbell Ritchie
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OP did say the compiler ran normally.
 
Petr Šlechta
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. I only did not know what else to check.
 
Petr Šlechta
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This also may be caused by wrong value in the CLASSPATH environment variable.
I would try
 
reply
    Bookmark Topic Watch Topic
  • New Topic