2.3. 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.
Remember, the src
directory you created in the previous section is
called the default package for source code. This directory
serves as the base directory for named packages that you will create.
You can think of it as the top-level directory for your package
directories (more soon). The default package can also contain classes
(and other types) that are not in a named package. When developing small
or temporary applications, it is a convenient place for package-less
.java
files
[2].
Note
You won’t need to use the cs1302
or hello
directories in this
step. You will use them in the next section.
Let’s dive in! Follow along on Odin and complete the steps below to create, compile, and execute a class in the default package. Once you are comfortable with these steps, move on to the next section to learn about named packages.
From within the
cs1302-packages
directory you created in the previous section, create a file in thesrc
directory calledHelloWorld.java
using the following command:emacs src/HelloWorld.java
Note the relative path used in the above command. Because we used
src/HelloWorld.java
, theHelloWorld.java
file will be placed within thesrc
directory (the default package for source code).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 theHelloWorld.java
file might be:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } // main } // HelloWorld
Now, we want to compile our program. We know that the source code is located in the default package for source code (
src
), but we want the compiled.class
file to go into the default package for compiled code (bin
). There are a few parts to this command but they are fairly descriptive once we break it down.Go ahead and exit Emacs and compile the
HelloWorld.java
file using the command below to run the Java compiler (javac
):javac -d bin src/HelloWorld.java
Understanding the Command
In English, the command above tells the Java compiler to: “compile the
HelloWorld.java
file located in thesrc
folder and place the resulting.class
file into thebin
folder. Note the use of relative paths in this command (bin
andsrc/HelloWorld.java
are relative to our current location.Run the
tree
command from withincs1302-packages
. You should see that theHelloWorld.java
file is in thesrc
folder and that theHelloWorld.class
file is in thebin
folder. Now, that’s organized!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). Since ourHelloWorld
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 assumes that the current working directory is the location of the default package for compiled code (where the.class
files are found). Since we are in thecs1302-packages
directory and not in the default package, 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 classes.
Here is the command to run the program:
java -cp bin HelloWorld
Understanding the Command
In English, the command above tells Java to: “run the
HelloWorld.class
file with FQNHelloWorld
and, if that file is not in the present working directory, it should check thebin
folder as well.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.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 replacebin
with any relative or absolute path to any directory that you have permission to write to. For example, if we had a directory calledclass_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.PROTIP:
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 theHelloWorld.java
file is not in that directory, we had to specify the relative pathsrc/HelloWorld.java
to tell the compiler how to find that file relative to the pwd. We’re telling the compiler “first, go into thesrc
directory, and then you’ll see the fileHelloWorld.java
”.We will use the Unix
tree
command (or the similarfind
) often in this course to see all files in a directory and its subdirectories. Go ahead and executetree
from within thecs1302-packages
directory. If you have followed the steps correctly up until now, you will see the following output: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.Let’s clean up! Delete the
HelloWorld.class
file that you created in thebin
folder (use therm
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.
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].
While types in the default package can access types in other
packages, the reverse is not true. That is, types in named packages
cannot access types in the default package.
In the next section, we will see how to compile our files to a named package.