0

I'm trying to run a simple java program with terminal:

   public class Test
   {
       public static void main(String...args)
       {
           System.out.println("Hello World");
       }
   }

So i compile using this command: javac Test.java and this produces the binary file Test.class. When I try to run the program with java Test I get this error:

Error: Could not find or load main class Test
Caused by: java.lang.ClassNotFoundException: Test

I don't know what to do anymore. When I run echo $JAVA_HOME I get the correct path:

/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home

I also tried to run using java -cp /path-to-Test.class Test and java .cp . Test but those also didn't work.

The program is not in a package. The program is in the desktop folder in macOS. What can I do to solve this problem?

1 Answers1

0

When you write "Test" use the whole package path.

like:

java com.some.dir.Test

for using with -cp: if you executing from Test location, you can use

java -cp . Test

else - you should mention the path

java -cp . com.some.dir.Test
Adir Dayan
  • 1,308
  • 13
  • 21