I'm trying to run my code in command prompt and it gives me error .. Can anyone know whats wrong on it?
Error: Could not find or load main class hello Caused by: java.lang.NoClassDefFoundError: FirstQuarter/hello (wrong name: hello)
I'm trying to run my code in command prompt and it gives me error .. Can anyone know whats wrong on it?
Error: Could not find or load main class hello Caused by: java.lang.NoClassDefFoundError: FirstQuarter/hello (wrong name: hello)
This type of error is due to the class not found in the Classpath during runtime but found during compile time. Look to print
System.getproperty("java.classpath")
which will print the classpath so you get an idea as to the actual runtime classpath.
Also, make sure you pass the fully qualified name of the class to "java" command which contains the main method for execution.
directory_that_holds_package>java package_name.Class_name
First, I take a guess that your program could run smoothly in Eclipse and Idea, but it gives this error in command line.
Now, you should include your program's package
in command line. If your program is like:
package firstprogram;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Then you should run java firstprogram.HelloWorld
in FirstQuarter folder.
This error is mainly due to when the program is not able to access the class which you have defined in your program and it can be due to reasons like you have not defined the proper classpath or you have not included the required library needed to run that class. The reasons can be many.
So try to run your code on any IDE as you will be able to easily identify the errors.