0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
aarelovich
  • 5,140
  • 11
  • 55
  • 106
  • Try adding the current directory to the classpath by adding `-cp .` to the `java` command. – Carl Mastrangelo May 13 '22 at 09:49
  • I tried doing this 'java Main -cp . ' and it was the exact same error – aarelovich May 13 '22 at 09:51
  • 1
    Also, Main and VMAPI should be in the same package. You don't need to use an `import` if they are peers of each other. In Java, the `package` usually matches the directory structure. Alternatively: you can fix it by running `mkdir VMAPI && mv VMAPI.class VMAPI` – Carl Mastrangelo May 13 '22 at 09:56
  • please check .class file in directory in which you are running this command.also please check comment of @CarlMastrangelo – Raushan Kumar May 13 '22 at 10:01
  • @CarlMastrangelo I found a solution in this answer: https://i.stack.imgur.com/d1PIl.png Which seems to do what you suggest as it creates de vmapi directory (swithced to lower case as per the answer's suggestion). Thank you it is now working! – aarelovich May 13 '22 at 10:12
  • The answer that worked for me: https://stackoverflow.com/questions/20365885/how-do-i-run-java-program-with-multiple-classes-from-cmd – aarelovich May 13 '22 at 10:13
  • If both classes are in the same directory, then that directory should be called `VMAPI`, and the `Main` class should also start with `package VMAPI;`. If that is the case, you need to go one directory up, and then you can use `java VMAPI.Main`. – Mark Rotteveel May 13 '22 at 10:36

1 Answers1

-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;