You need to do two things.
Replace your current import with import mypackage.child
The import needs to reflect the fully qualified name of the class(es) you're importing. It has nothing to do with the location of the class files on your machine. You could also do import mypackage.*
to import all classes from package mypackage
instead of specifying the one(s) you want individually... that's just a coding style choice.
Add subdirectory
to your classpath
The classpath, on the other hand, does have to do with the location of class files on your machine. As the name implies, it lists the paths to all of the places where the JVM should look for classes that your program uses. Or, as Oracle puts it, "The class path tells JDK tools and applications where to find third-party and user-defined classes." You don't need to tell Java where to find the special classes that it comes with (like String
).
The instructions for #2 depend on how you're running your program (from the command line or Eclipse or something else). Since you're using the command line (per your comment), you'll need to use the -classpath
flag, or its shorthand, -cp
, like so: java -cp ./subdirectory main