aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package vs directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "package vs directory" Watch "package vs directory" New topic
Author

package vs directory

Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
Can someone please explain the difference between package and directory and how this affects class file interaction and compilation.
I always thought that each package name would in essence be a directory or subdirectory, but it seems from some mock exam examples that different packages can be in the same directory.
If that is so then can different files from the same package be in different directories?
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Hi Damien,
Have a look at your directory structure where you have installed your java sdk.
Mine looks like this: c:\j2sdk1.4.1_01
When you download the sdk you get a file called src.zip. If you extract that zip file to c:\j2sdk1.4.1_01\src then you can see how the packages are arranged. You can also see that the 'java' package also has many other packages in that 'directory'. The javax package has many other packages in the javax directory.
Does this make sense?


Thanks, leo
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
But how do packages and files etc know how to find one another if the package name does not correspond to any kind of directory name?
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Originally posted by Damien Howard:
But how do packages and files etc know how to find one another if the package name does not correspond to any kind of directory name?

Hmm, do you mean like: c:\myjavaprograms\classprojectA\HelloWorld.java ?
How does class HelloWorld find other classes in other packages?
This is how
If you still have questions, just ask.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Damien. I do not know enough on this topic to attempt a direct and concise, correct and complete answer. But I suspect you would find some clues to your question in the documentation for the compiler and the interpreter, javac.html and java.html. After all the programs javac and java need to map type names to file names.
For example, here are a few things I found:
You should arrange source files in a directory tree that reflects their package tree.
For example, if you keep all your source files in \workspace, the source code for com.mysoft.mypack.MyClass should be in \workspace\com\mysoft\mypack\MyClass.java.
Java classes are organized into packages that are mapped to directories in the file system.
Note: An interesting consequence of the package specification mechanism is that files that are part of the same package may actually exist in different directories. The package name will be the same for each class, but the path to each file may start from a different directory in the class path.
Classes can be stored either in directories (folders) or in archive files.
If the class com.mypackage.MyClass is stored under /myclasses, then /myclasses must be in the user class path and the full path to the class file must be /myclasses/com/mypackage/MyClass.class.
If the class is stored in an archive named myclasses.jar, then myclasses.jar must be in the user class path, and the class file must be stored in the archive as com/mypackage/MyClass.class.
Also, you might take a look at classpath.html and doc\tooldocs\findingclasses.html
[ July 09, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
different packages can be in the same directory

Can you show an example of this?
can different files from the same package be in different directories?

Yes. See Note: in my previous reply.
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
I don't actually have an example of different packages in the same directory, I just remember reading a mock exam question that stated this.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example where classes from two different packages reside in the same directory.
My working directory is
C:\Documents and Settings\Marlene\My Documents\Java\Exam\packages
Two files reside in the same subdirectory
libA\libB\util\IOStream.java
libA\libB\util\LinkedList.java
package libB.util;
public class IOStream {
public static void main(String[] args) {
System.out.println(new IOStream().getClass().getName());
}
}
package libA.libB.util;
public class LinkedList {
public static void main(String[] args) {
System.out.println(new LinkedList().getClass().getName());
}
}
>javac libA\libB\util\IOStream.java
>javac libA\libB\util\LinkedList.java
>java -classpath libA libB.util.IOStream
libB.util.IOStream
>java -classpath . libA.libB.util.LinkedList
libA.libB.util.LinkedList
The trick is to play with the classpath. In a more exotic example, you probably also have to use the classpath when compiling.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example where classes from the SAME packages reside in DIFFERENT directories.
My working directory is
C:\Documents and Settings\Marlene\My Documents\Java\Exam\packages
The files are in subdirectories
libC\Math.java
libA\libB\libC.Date.java
package libC;
public class Math {
public static void main(String[] args) {
System.out.println(new Math().getClass().getName());
}
}
package libC;
public class Date {
public static void main(String[] args) {
System.out.println(new Date().getClass().getName());
}
}
>javac libC\Math.java
>javac libA\libB\libC\Date.java
>java -classpath . libC.Math
libC.Math
>java -classpath libA\libB libC.Date
libC.Date
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
In summary, all the while, I have been adhering to the convention (rule?) that a class in package p1.p2.p3 must reside in a subdirectory p1/p2/p3 of some arbitrary directory.
I have been using the classpath to point to different packages in the same directory or to the same package in different directories.
As I said, I do not know much about this stuff. But I suspect this is essence of the problem.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
You might be interested in this section in the JLS.

JLS 7.2 Host Support for Packages
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
Ok thanks for the examples Marlene
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package vs directory