• Post Reply Bookmark Topic Watch Topic
  • New Topic

NoClassDefFoundError when using -cp  RSS feed

Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I've always used NetBeans to compile my code, but I'm preparing for the OCPJP certification, so I'm now using the command line. I've encountered a very strange issue.

I have two classes in two separate files (both of them have been compiled succesfully). They look as follows:

My directory structure is as follows:

When I run the Java command from the test directory without specifying a classpath (i.e. java Main), I get a NoClassDefFoundError because the HelloPrinter class cannot be found. This is what I expected to happen. However, when try to add the HelloPrinter class to the classpath by running java -cp classes Main, I get a NoClassDefFoundError stating that the Main class cannot be found. Does anyone have an idea why this is happening? Am I missing something obvious?

Many thanks.


Edit: Here's the error.

java -cp classes Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Main. Program will exit.

Another edit: I've just found the issue. I just needed to include the current directory in the classpath as well. When I tried it earlier I was using the Unix path separator ( instead of the Windows separator (;), and it didn't work.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!