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:

  1. Place the .java file in the appropriate package directory; and

  2. Include 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.

  1. 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.

  2. Move the HelloWorld.java file into the cs1302.hello package directory. You may need to create both the cs1302 and hello directories before executing the following command.

    If you are not in the cs1302-packages directory, this command will not work
    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.

  3. 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.

  4. Compile the program (don’t forget tab completion - you don’t want to type the whole command):

    If you are not in the cs1302-packages directory, this command will not work.
    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 the src/cs1302/hello folder and place the resulting .class file in the bin folder.

  5. Execute the tree or find command. Note that the HelloWorld.class file was created under bin/cs1302/hello. The output of tree should look the same as the output below. Notice that the compiler automatically created the necessary package directories for our compiled code under bin! 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 your tree or find output to Piazza.

    Important

    If your HelloWorld.class file is located under the bin folder and not in the package directory, check the package statement in your HelloWorld.java file.

    The output of the ``tree`` command after compiling to a named package
  6. Run the program using the java command:

    If you are not in the cs1302-packages directory, this command will not work.
    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 FQN cs1302.hello.HelloWorld and, if that file is not in the present working directory, it should check the bin folder as well. Notice that we do not need to tell it the subdirectories within bin 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.

  7. 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

IMAGE ALT TEXT