Error: Error: Could not find or load main class HelloWorld | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Error: Error: Could not find or load main class HelloWorld

I have installed JDK as well as JRE on my Windows system. I also have set path to the Java compiler. But when I try to run even a simple Hello World program, it compiles successfully but while executing it gives the following error. Error: Error: Could not find or load main class HelloWorld I have googled the above error and concluded that it arises when 1. The class name is different from what we are passing to java command. 2. Classpath is wrong. 3. Package path is wrong. 4. There is actually no main method. I am sure that it's no 1, 3 or 4 as I am only trying to run a simple Hello World program. Though I've checked, but then also I feel a little chance of 2. Can somebody help me out with this.

28th Aug 2020, 4:19 PM
Shashank Shekhar
Shashank Shekhar - avatar
10 Answers
+ 1
you can also specify classpath as option javac -d .class HelloWorld.java java -cp .class HelloWorld code and your way of execute is right
28th Aug 2020, 10:34 PM
zemiak
+ 1
In case, the Hello class is compiled into a .class file in the current directory — yet the java command can't seem to find it....? Answer . A class is only found if it appears in the class path. By default, the class path consists of the current directory. If the CLASSPATH environment variable is set, and doesn't include the current directory, the launcher can no longer find classes in the current directory. The solution is to change the CLASSPATH variable to include the current directory. For example, if the CLASSPATH value is c:\java\newLibrary.jar (Windows) or /home/me/newLibrary.jar (UNIX or Linux) it needs to be changed to .;c:\java\newLibrary.jar or .:/home/me/newLibrary.jar. Got it from Oracle lesson path setting.. You are not in current directory so change it to the above way..
28th Aug 2020, 8:22 PM
Jayakrishna 🇮🇳
+ 1
Shashank Shekhar May be that's the problem, bcoz I faced same problem my JRE was 6 and JDK was 7, so I updated JRE to 7 as well. Try using same version and from JDK 7 JRE is given along with JDK
29th Aug 2020, 8:08 AM
Alaska
Alaska - avatar
0
can we see your code ? can you manually find your .class file? what is your whole command to execute it? do you use (cmd) terminal ?
28th Aug 2020, 4:49 PM
zemiak
0
The epic error almost every java programmer face😂 Bruh, see we can only help you if you provide pics of your code, your classpath, and how you execute. Is your classpath directed towards bin folder? What JDK version you are using? As JDK upto 6 doesn't provide JRE along with JDK, so you need to give path of JRE also, if JDK 7 or later no worries. How are executing java command in cmd? Can you show us program?
28th Aug 2020, 5:33 PM
Alaska
Alaska - avatar
0
//HelloWorld.java class HelloWorld { public static void main(String ar[]) { System.out.println("Hello World"); } } To compile: javac HelloWorld.java To execute: java HelloWorld After compiling, a .class file is generated. Right now, I don't have that system on which error was occurring and so I'll post the classpath in the morning.
28th Aug 2020, 6:01 PM
Shashank Shekhar
Shashank Shekhar - avatar
0
String args[] typo?
28th Aug 2020, 6:02 PM
Alaska
Alaska - avatar
0
Nopes. I've used it so many times before. If that was the error then it would have given compile-time error. @Alaska
28th Aug 2020, 6:11 PM
Shashank Shekhar
Shashank Shekhar - avatar
0
No wait you are right actually any name is allowed there, sorry😅 JDK version?
28th Aug 2020, 6:14 PM
Alaska
Alaska - avatar
0
@zamiak that way of compiling and executing worked for me. @JayaKrisha Your answer helped me understand why I was facing that problem. I checked the CLASSPATH environment variable. There is something wrong there. I tried to add .; in the beginning but it didn't work. Right now, only xampp's path is there and adding .; didn't work. @Alaska That machine is using JDK 1.7 but the JRE is 1.8. I don't know-how is that because I wasn't using that system before.
29th Aug 2020, 7:54 AM
Shashank Shekhar
Shashank Shekhar - avatar