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.
10 Respostas
+ 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
+ 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..
+ 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
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 ?
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?
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.
0
String args[] typo?
0
Nopes. I've used it so many times before. If that was the error then it would have given compile-time error. @Alaska
0
No wait you are right actually any name is allowed there, sorryđ
JDK version?
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.