'Exception java.lang.ClassNotFoundException when importing a class in my local dir [duplicate]

I created two files in the same directory.

One is called VMAPI.java

package VMAPI;

public class VMAPI {
   public VMAPI() {

      System.out.println("Created VMAPI");

   }

} 

The other one is called Main.java and looks like this:

 import VMAPI.VMAPI;
 
 public class Main {

    public static void main (String[] args){

       VMAPI vmapi = new VMAPI();

    }


 }

I compile this using the command

javac *.java

And Run using

java Main

When I do, I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: VMAPI/VMAPI
        at Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: VMAPI.VMAPI
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more

I understand the error is telling me that it can't find the VMAPI class, but I don't understand why.

All I want to do is create maybe 2 or 3 classes for a very simple example use in my main function and that's it. I don't wan to use an IDE and want to compile, preferably with the command line.



Solution 1:[1]

As per the Java recommendation, you should not have this type of naming convention. Package name should be in lower case and class name should start with capital letter (camel) and try to have different names.

for example: package name - vmapi class name - VmapiClass (this is just an example but you know the context)

then your import should be like this.

import vmapi.VmapiClass;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kumar Deepak