2.4. Named Package¶
Now let’s create a named package. To place a class (or interface) in a named package, you must do two things:
Place the
.java
file in the appropriate package directory; andInclude a
package
statement at the top of the.java
file.
Let’s dive in! Follow along on Odin and complete the steps below
to move the HelloWorld
class into the cs1302.hello
package, compile
it, and run it.
If you aren’t already there, change into the
cs1302-packages
directory. Make sure the directory is in the same state we left it in at the end of the previous section.Move the
HelloWorld.java
file into thecs1302.hello
package directory. You may need to create both thecs1302
andhello
directories before executing the following command.mv src/HelloWorld.java src/cs1302/hello/
Understanding the Command
The command above moves the
HelloWorld.java
file from the default package (src
) into a directory that corresponds to the named package we are moving it into (cs1302/hello
).Notice that the package directory (
cs1302/hello
) is directly inside of the default package (src
) and that the names of the folders match the package names. This structure will be the same whenever a file is in a named package. Of course, the folder names (including the name of the default package folder) are subject to change.Completing this step satisfies the first requirement for placing a class in a named package.
Using Emacs, edit the
HelloWorld.java
file and add the following package statement at the top:package cs1302.hello;
In Java, a package statement, if included, must be the first line of code in the file (i.e., excluding comments and white space).
Go ahead and close Emacs. Completing this step satisfies the second requirement for placing a class in a named package.
That’s it! Now, let’s compile and run the code.
Compile the program (don’t forget tab completion - you don’t want to type the whole command):
javac -d bin src/cs1302/hello/HelloWorld.java
Understanding the Command
This command is almost identical to the compilation command in the previous section that compiled code in the default package. The only change is the relative path to the file.
In English, this command tells the Java compiler to: “compile the
HelloWorld.java
file located in thesrc/cs1302/hello
folder and place the resulting.class
file in thebin
folder.Execute the
tree
orfind
command. Note that theHelloWorld.class
file was created underbin/cs1302/hello
. The output oftree
should look the same as the output below. Notice that the compiler automatically created the necessary package directories for our compiled code underbin
! It was able to do this because we correctly added the package statement to the top of the source file.Note: If you see any tilde (~) files, those are just backup copies of older versions of your files. You can ignore those. If you see any other differences between your output and the output below, you likely have a small error in a command or your package statement in the
HelloWorld.java
file. Double-check that you did everything correctly and, if you need assistance, post a screenshot of yourtree
orfind
output to Piazza.Important
If your
HelloWorld.class
file is located under thebin
folder and not in the package directory, check thepackage
statement in yourHelloWorld.java
file.Run the program using the
java
command:java -cp bin cs1302.hello.HelloWorld
Understanding the Command
This command is almost identical to the compilation command in the previous section that compiled code in the default package. The only change is the FQN. Note that the package is now part of the FQN!
In English, this command tells Java to: “run the
HelloWorld.class
file with FQNcs1302.hello.HelloWorld
and, if that file is not in the present working directory, it should check thebin
folder as well. Notice that we do not need to tell it the subdirectories withinbin
as those are implied by the FQN.PROTIP: Although packages correspond to directories, a fully qualified name (FQN) uses
.
(dot) for the name separator and not a slash.Test Yourself
Change directories into your home directory and see if you can run
HelloWorld.class
from that location. Note: The relative path you provide as part of the classpath-cp
will have to change. What about the FQN? Write the command you used in your notes.Congratulations on compiling your code to a named package! Don’t delete your work unless you want to work through the tutorial again for extra practice. The next section will continue where this one left off.
Here is a video outlining the steps in this section if you get stuck:
Additional Practice - Video
If you’d like some extra practice compiling and running code in a Unix environment, follow along with Dr. Barnes as he works through another example.
https://www.youtube.com/watch?v=vuenX567T6c