How to compile java package structures using javac

The issue was that the class path needs to be set for each command (javac and java):

Attempted Steps

  1. instead of going to subpackage, compile HelloWorld.java from the top_level:

    $javac -cp . importpackage/subpackage/HelloWorld.java

  2. compile CallPackage.java in the same way:

    $javac -cp . CallPackage.java

  3. run the file using the class path also:

    $java -cp . CallPackage

NOTE: running “$java CallPackage” will give an error “Error: Could not find or load main class CallPackage”

In summary, during each step, the class path must be specified. It worked after running it as such.

Leave a Comment