andrefurquin
andrefurquin

Reputation: 522

Do I have to import a class that is in the same package but in different folders?

I have some classes that are located in classes/com/scja/exam/tutorial/planets on the filesystem. I'm trying to compile a file that is located in classes/com/scja/exam/tutorial/. Do I have to manually import this ? I'm trying to compile using this command:

javac -d classes -cp classes/com/scja/exam/tutorial/planets/:. src/com/scjaexam/tutorial/GreetingsUniverse.java

It seems like java cannot find the classes this file needs.

Upvotes: 1

Views: 1493

Answers (2)

Hot Licks
Hot Licks

Reputation: 47699

Understand that when Java searches for a class named aaa.bbb.ccc.MyClass, it searches each directory in the classpath for a directory named "aaa". Finding one, it will search that directory for "bbb", then, if that's found "ccc", then actually look for "MyClass.class". If you make your classpath -cp aaa/bbb/ccc then Java will look there, find no "aaa", and give up.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691635

A class in a package must import classes it uses (without using their fully qualified name) that are not in the same package (an not in java.lang). The directories where the classes are stored must match the packages, but you could have several root directories (or jars) containing the classes.

Your command doesn't work because you put the directory of a package (classes/com/scja/exam/tutorial/planets/) in the classpath, instead of putting the root directory (classes).

Upvotes: 2

Related Questions