1

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)

enter image description here

PSKP
  • 1,178
  • 14
  • 28
Xen
  • 105
  • 9
  • Does this answer your question? [How to fix java.lang.NoClassDefFoundError in Command Prompt?](https://stackoverflow.com/questions/50882074/how-to-fix-java-lang-noclassdeffounderror-in-command-prompt) – Ramana Jun 29 '20 at 04:16

3 Answers3

0

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
  • It give me a new error after adding the package name.. Could not find or load main class FirstQuarter.hello Caused by: java.lang.ClassNotFoundException: FirstQuarter.hello – Xen Jun 29 '20 at 04:33
  • @AisaHassan please show the complete error log. It could be that you are running the command from the wrong directory. –  Jun 29 '20 at 04:43
  • D:\xen>java FirstQuarter.hello Error: Could not find or load main class FirstQuarter.hello Caused by: java.lang.ClassNotFoundException: FirstQuarter.hello – Xen Jun 29 '20 at 04:46
  • @AisaHassan your running it from xen but I see you compiled in xen also. Typically you compile in your package then go into the directory that holds your package and run the fully qualified name: java package_name.class_name –  Jun 29 '20 at 04:55
  • D:\xen\test1\src\FirstQuarter>java FirstQuarter.hello Error: Could not find or load main class FirstQuarter.hello Caused by: java.lang.ClassNotFoundException: FirstQuarter.hello – Xen Jun 29 '20 at 05:03
  • @AisaHassan go into src directory and run the same command. You have to be outside the package. –  Jun 29 '20 at 05:07
  • Assuming FirstQuarter is your package which holds a hello.class file. Go to src directory, example: D:\xen\test1\src>java FirstQuarter.hello –  Jun 29 '20 at 05:17
  • @AisaHassan your welcome. Please help the community by upvoting my answer. This will help others with the same issue to find my answer. Thanks. –  Jun 29 '20 at 05:36
0

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.

Roy Yang
  • 31
  • 2
  • Now it gives me Error: Could not find or load main class FirstQuarter.hello Caused by: java.lang.ClassNotFoundException: FirstQuarter.hello – Xen Jun 29 '20 at 04:29
  • Your file hello should be put under FirstQuarter folder. (Although it is not an issue but better follow naming conventions so that everybody will be able to understand what is what. Package names should be in lowercase and class name should be in PascalCase) – Bala Jun 29 '20 at 14:17
0

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.

Aman Singh
  • 23
  • 7