Search code examples
javaclasspathjavac

Java: could not find or load main class test


I have two .java files in a directory. One of them is an implementation of the Vector data structure, the other is a test class that contains a main method to test my Vector implementation. I'm trying to compile both files at the same time, so I did javac -cp . *.java, then when I run java test, I get Error: Could not find or load main class test, even though after compiling, I now have two .java files and two .class files. What do I do in order to run test from terminal?

Following is my test.java file:

package mStructures;

//import java.util.Vector;

public class test {

  public static void main(String argv[]){
  Vector<Integer> testV = new Vector<Integer>();
    for(int i = 0; i < 39; i++){
      System.out.printf("Capacity before adding %d: %d%n", i, testV.capacity());
      testV.add(i);
      System.out.printf("Capacity after adding %d: %d%n", i, testV.capacity());
    }
  }

}

Solution

  • For simple things it's best to have Test.java (no package, note the import)

    import mStruct.Vector;
    public class Test { ... main ... }
    

    in some directory and

    package mStruct;
    public class Vector { ... }
    

    in its subdirectory mStruct.

    Compile and exec:

    javac Test.java mStruct/*.java
    java Test