Up to now, all the code we’ve written in this chapter has been in the default package. This is a special unnamed package that you should use only for throwaway code. You can tell the code is in the default package, because there’s no package name. On the exam, you’ll see the default package used a lot to save space in code listings. In real life, always name your pack-ages to avoid naming conflicts and to allow others to reuse your code.
Now it’s time to create a new package. The directory structure on your computer is related to the package name. In this section, just read along. We cover how to compile and run the code in the next section.
Suppose we have these two classes:
package packagea;
public class ClassA {}
package packageb;
import packagea.ClassA;
public class ClassB {
public static void main(String[] args) {
ClassA a;
System.out.println(“Got it”);
}
}
When you run a Java program, Java knows where to look for those package names. In this case, running from C:\temp works because both packagea and packageb are underneath it.
You’ll learn Java much more easily by using the command line to compile and test your examples. Once you know the Java syntax well, you can switch to an IDE. But for the exam, your goal is to know details about the language and not have the IDE hide them for you.
Follow this example to make sure you know how to use the command line. If you have any problems following this procedure, post a question in the Beginning Java forum at CodeRanch. Describe what you tried and what the error said.
www.coderanch.com/forums/f-33/java
The first step is to create the two files from the previous section. Table 1.1 shows the expected fully qualified filenames and the command to get into the directory for the next steps.
TABLE 1. 1 Setup procedure by operating system | |||
Step | Windows | Mac/Linux | |
1. Create first class. | C:\temp\packagea\ | /tmp/packagea/ClassA.java | |
ClassA.java | |||
2. Create second class. | C:\temp\packageb\ | /tmp/packageb/ClassB.java | |
ClassB.java | |||
3. Go to directory. | cd C:\temp | cd /tmp | |
Now it is time to compile the code. Luckily, this is the same regardless of the operating system. To compile, type the following command:
javac packagea/ClassA.java packageb/ClassB.java
If this command doesn’t work, you’ll get an error message. Check your files carefully for typos against the provided files. If the command does work, two new files will be created: packagea/ClassA.class and packageb/ClassB.class.
Compiling with Wildcards
You can use an asterisk to specify that you’d like to include all Java files in a directory. This is convenient when you have a lot of files in a package. We can rewrite the previous javac
command like this:
javac packagea/*.java packageb/*.java
However, you cannot use a wildcard to include subdirectories. If you were to write javac *.java, the code in the packages would not be picked up.
Now that your code has compiled, you can run it by typing the following command:
java packageb.ClassB
If it works, you’ll see Got it printed. You might have noticed that we typed ClassB rather than ClassB.class. As discussed earlier, you don’t pass the extension when running a program.
Figure 1.1 shows where the .class files were created in the directory structure.
FIGURE 1. 1 Compiling with packages
packagea
ClassA.java
ClassA.class
packageb
ClassB.java
ClassB.class