user3284469
user3284469

Reputation:

Can two .class files stored under arbitrarily different paths belong to the same package?

Two .java files A.java and B.java both have this line:

package mypackage

If I would like A.class and B.class both belong to the same package mypackage, do I need to follow some rules to store A.class and B.class?

Can I store A.class as /pathnametoA/A.class and B.class is stored under /pathnametoB/B.class, where pathnametoA and pathnametoB are arbitrary pathnames?

For example, someone wrote B.java as a Junit test file to test A.java, and they both belong to the same package mypackage. I thought A.class and B.class would be stored under the same directory mypackage, but A.class is actually stored under /commondpath/bin/mypackage and B.class under /commonpath/test/mypackage. Can A.class and B.class belong to the same package mypackage?

Note that I use javac and java in command line in Ubuntu.

Thanks.


Update:

Is a package in Java defined as all the .class files under a directory?

So there is a one-to-one correspondence between packages and pathnames of directories?

Upvotes: 0

Views: 56

Answers (2)

user207421
user207421

Reputation: 310840

Two .java files A.java and B.java both have this line:

package mypackage

If I would like A.class and B.class both belong to the same package mypackage,

They already do. You said so in the source file.

do I need to follow some rules to store A.class and B.class?

You need to store them both in a directory called mypackage.

Can I store A.class as /pathnametoA/A.class and B.class is stored under /pathnametoB/B.class, where pathnametoA and pathnametoB are arbitrary pathnames?

Not unless you satisfy the condition mentioned above. They won't be found. You will get ClassNotFoundException.

For example, someone wrote B.java as a Junit test file to test A.java, and they both belong to the same package mypackage. I thought A.class and B.class would be stored under the same directory mypackage, but A.class is actually stored under /commondpath/bin/mypackage and B.class under /commonpath/test/mypackage. Can A.class and B.class belong to the same package mypackage?

Yes, if both commonpath/bin and commonpath/test are mentioned in the CLASSPATH.

Is a package in Java defined as all the .class files under a directory?

No, it is defined as all the classes that have the same package statement.

So there is a one-to-one correspondence between packages and pathnames of directories?

No, as per your example.

Upvotes: 3

Andreas
Andreas

Reputation: 159086

The JVM loads classes using a Class Loader. The default class loaders use a CLASSPATH and the full name of the class to find the .class file.

In your case, if CLASSPATH references both /commondpath/bin and /commonpath/test, it will look in those two folders for the files mypackage/A.class and mypackage/B.class.

The CLASSPATH identifies the root folders of the package hierarchy. You cannot store A.class in a folder named pathnametoA.

Well, not unless you install your own Class Loaders for lookup up the .class files using a different scheme, and that is really not a good idea.

See section "Class Path and Package Names" of chapter 2 "Setting the Class Path document" of the "Java Platform, Standard Edition Tools Reference".

Upvotes: 1

Related Questions