Search code examples
javaunixjavac

Unix command line java Error: Could not find or load main class


I have java and javac installed on a unix server at my school.

I have a java file called Test.java that looks like:

package Spill;
import java.util.Scanner;

public class Test {

  public static void main(String[] args) {

  }
}

Test calls a class called Grid in a file called Grid.java
and Grid calls a class called Cell in a file called Cell.java

All three files are in a directory called Assignment

While in Assignment I use the command:

javac Test.java Grid.java Cell.java

and three files appear called Test.class Grid.class and Cell.class appear.

I set all files in directory to have read, write and execute permissions.

Finally I use the command:

java Spill.Test

and get the error

Error: Could not find or load main class Spill.Test

I also tried

java Test

and got the same error

Error: Could not find or load main class Test

What am I missing?

Edit: Not an exact duplicate, since the linked article had all the possible reasons for the error message, but I have information in my question that tells us it could only be not creating a sub-directory to correspond with the package name.


Solution

  • Your three classes must be in folder named Spill relative to your current folder, and you need to add that to your CLASSPATH (either with . or in full). The package is part of the class name in Java and must mirror on the disk storage. Assuming you're currently in the Spill folder,

    cd ..
    java -cp . Spill.Test