The key to master the package mecanism: a class is found if the pakage names reflect a directory structure that appendded to any entry in the classpath gives the location of the class (in that package). If the class is in the default package it must be placed in any directory cited in the classpath.
The classpath, if not set, defaults to the current directory. Thus, compiling from mypackage directory the class B, which is in a package named mypackage, makes the compiler to looks for B in a non existing directory mypackage/mypackage. This is the entry in the classpath plus the package.
I placed A in
test and B in test/mypackage . It compiled with "javac -classpath . mypackage.B" from test. The compiler found A in the current directory and B in test/mypackage.