2.2. Default Package

Prerequisite Content

This section assumes that the reader is familiar with the content of the “Executing Programs” video (Video #1 under “1301 Videos” on eLC). If you have not watched that video, please take a few minutes to do so now.

In this section, we will see how to compile a Java file using the default package as a warm-up (i.e., we will compile a class in a package-less .java file). This Java file will not be contained within a named package. In the next section, we will see how to add the file to a named package.

2.2.1. Setting up the Environment

Learn by Experimenting!

For each of the remaining sections in this chapter, be sure to login to Odin and type out the commands given in each part, taking notes as needed. When you are done, you will have a step-by-step outline for creating packages (both named and unnamed) in Java. Your notes and will be very useful on upcoming assignments and projects.

On Odin, complete each of the following steps:

  1. From your home directory, create a directory for this tutorial called cs1302-packages and change into it using the following commands:

    Changes to your home directory and creates the directory for this tutorial.
    cd ~
    mkdir cs1302-packages
    cd cs1302-packages
    
  2. Use the mkdir command to create a src directory and a bin directory within cs1302-packages. When you are done, you should have the following subdirectory structure for the source code and compiled code we will create shortly:

    cs1302-packages
     |--- bin
     |--- src
    

    Each directory above plays a role in the example. Here is a breakdown of the different subdirectories and their roles:

    • cs1302-packages is the top-level assignment/tutorial directory. We create this folder so you have a place to organize all of the work you do in this tutorial. You will have similar directories for homeworks, projects, and other tutorials throughout the course.

    • src will contain our .java files in this section. Since we are not (yet) compiling to a named package, we will place the files directly in src which is known as the default (no-name) package for source code.

    • bin will contain our .class files in this section. Since we are not (yet) compiling to a named package, we will place the files directly in bin which is known as the default (no-name) package for compiled code.

  3. If you aren’t already in the cs1302-packages directory, change into it and run the tree command to make sure the directory contains all of the required subdirectories and that those subdirectories are organized correctly.

Short Video Demo of the Steps Above

Here is a short video demonstrating these steps in case you get stuck:

2.2.2. Creating a Program

Remember, the src directory you created in the previous section is called the default package for source code. In this section, we will place our source code directly in the default (no-name) package. When developing small or temporary applications, it is a convenient place for package-less .java files [2].

Let’s dive in!

  1. From within the cs1302-packages directory you created in the previous section, create and open file in the src directory called HelloWorld.java using the following command:

    Execute this command from within cs1302-packages.
    emacs src/HelloWorld.java
    

    Note the relative path used in the above command. Because we used src/HelloWorld.java, the HelloWorld.java file will be placed within the src directory (the default package for source code).

  2. You should be presented with a blank file in the Emacs editor. Now, create a basic “Hello, World!” program in that file. The class should be called HelloWorld to match the file name. For example, the contents of the HelloWorld.java file might be:

    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        } // main
    } // HelloWorld
    

2.2.3. Compiling the Program

  1. We have already placed the source code in the default package for source code (src). When we compile, we want the compiled .class file to end up in the default package for compiled code (bin). This helps us keep the .java files separate from the .class files.

    Go ahead and exit Emacs and compile the HelloWorld.java file using the command below to run the Java compiler (javac):

    If you are not in the cs1302-packages directory, this command will not work
    javac -d bin src/HelloWorld.java
    

    Understanding the Command

    -d stands for destination. The syntax is to put -d followed by the location where we want the resulting .class files to be placed (their destination).

    In English, the command above tells the Java compiler to: “compile the HelloWorld.java file located in the src folder and place the resulting .class file into the bin folder. Note the use of relative paths in this command (bin and src/HelloWorld.java are relative to our current location.

  2. Run the tree command from within cs1302-packages. You should see that the HelloWorld.java file is in the src folder and that the HelloWorld.class file is in the bin folder. Now, that’s organized!

2.2.4. Running the Program

  1. Now that the class is compiled, we should note a few important things before running the program:

    • The java command requires the FQN of the class (not the name of the .class file).

      Note

      Since our HelloWorld class is not in a named package (we compiled to the default package), its FQN is the same as its simple class name, HelloWorld.

    • When executing the java command, Java looks in your current working directory for the .class files it needs to run. Since we are in the cs1302-packages directory, it will not be able to find the .class files unless we tell it where to find them. For this reason, we need to specify the default package for compiled code (bin) using a command line option called -cp for class path. As its name suggests, the class path is the path to the default package for compiled code.

    Here is the command to run the program:

    This command will not work if you aren’t in the cs1302-packages directory.
    java -cp bin HelloWorld
    

    Understanding the Command

    In English, the command above tells Java to: “run the HelloWorld class located in the default package and check the bin folder for the .class file it needs.

    You can execute a Java program anywhere on the system as long as you know the fully qualified name of the class containing the main method and the location of that compiled class’s associated default package, assuming proper file permissions.

    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.

    Test Yourself Solution (Open after attempting the question above)

    Once you are in your home directory, the command to run HelloWorld will have to change. Specifically, you need to put the location of the default package for compiled code on the classpath (using a relative or absolute path). The FQN of the class does not change.

    This command assumes that the cs1302-packages directory is in your home folder
    java -cp cs1302-packages/bin HelloWorld
    

    Important Note: bin is just the relative path to the directory where the compiled .class files will be placed. In our example, bin is a directory located inside of the present working directory. You can replace bin with any relative or absolute path to any directory that you have permission to write to. For example, if we had a directory called class_files in our present working directory, we could compile the code with -d class_files and our .class files would be placed in that directory.

    Pro Tip

    When you’re first starting out in a Unix environment, you might feel lost at times. Remember, it’s always important to know your present working directory (pwd) as that is the location where the commands you enter will be run. In the commands above, our pwd is cs1302-packages. Since the HelloWorld.java file is not in that directory, we had to specify the relative path src/HelloWorld.java to tell the compiler how to find that file relative to the pwd. We’re telling the compiler “first, go into the src directory, and then you’ll see the file HelloWorld.java.

  2. We will use the Unix tree command (or the similar find) often in this course to see all files in a directory and its subdirectories. Go ahead and execute tree from within the cs1302-packages directory. If you have followed the steps correctly up until now, you will see the following output:

    The output of the ``tree`` command after compiling to the default package

    The output of the tree command shows that we successfully separated the source code (src) from the compiled code (bin). If you see any tilde (~) files, those are just backup copies of older versions of your files. You can ignore those.

  3. Let’s clean up! Delete the HelloWorld.class file that you created in the bin folder (use the rm command with a valid relative path to the file. Remember to use tab completion so you don’t have to type long filenames! In general, you don’t need to delete your .class files, but we will do so in this example because we will compile the code to a different directory in the next section.

Short Video Demo of the Steps Above

Here is a video outlining the steps in this section if you get stuck:

Remember, source code should only be placed directly in the default package directory (src) for convenience when developing small or temporary applications or when just beginning development [2].

In the next section, we will see how to compile our files to a named package.