Lecture 04: Namespaces, Packages and the CLASSPATH
Announcements
Piazza: You should not post your code in public posts! Doing so is a violation of the Academic Honesty Policy! Use private posts instead. I will disable the ability to post anonymously if this becomes a problem.
Consider starting assignments earlier. I know that for many of you the first assignment was pretty easy, but for many of you it was not. I got a lot of emails and private questions on Piazza last night.
Again: If you’re a late add, you must contact me about what you need to do to catch up. SPIRE does not notify me about late adds, and the CS main office only knows about overrides into the course. If you used SPIRE to add yourself and haven’t been in touch with me, I have no idea that you’ve added late.
Finally, add your student ID to gradescope, or you’re going to start seeing 0s in Moodle’s gradebook.
Namespaces
Many programming languages, including Java incorporate the idea of a “namespace”. A namespace is a way to provide context to a particular name.
For a real-world analogy, you might think of a person’s name, say, “Zachary”. There might be more than one in this class, so we add some context (a surname, or a student ID, or an address, or all of the above) to disambiguate which we mean.
This is very similar to the idea of a variable’s scope in Java, but slightly different, as it’s how we precisely name and identify classes.
For example, we all write System.out.println()
all the time, and we all know that System is probably a class, since it starts with a capital letter. Where does it come from, though?
Packages
It’s part of the java.lang
package. Java organizes classes into packages; which are a hierarchical sequence of tokens (words), separated by dots. The built-in parts of the Java standard library all are part of the “java.” package, though it’s further subdivided.
For example, the aforementioned java.lang
package defines the classes that are fundamental to the design of the language itself: things like System
and String
are defined here.
http://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html#package.description
By default, things in this namespace are automatically “imported” into the local namespace. That is, you don’t have to type java.lang.String
to declare a String
(though you can); String
suffices.
Interestingly, it’s not against the rules to define your own System
class. But it’s like if you have both an instance variable named x
and a local variable named x
. By default, Java assumes you want to “more local” one.
int x = 5;
void test() {
x = 3;
System.out.println(x);
System.out.println(this.x);
}
To get the “outer” one, you need to prefix it with this.
, which tells Java you want the current instance variable with the same name.
Similarly, with a class, you need to fully specify the class if you want access to it. Inside your custom System
class, if you want to access the “normal” System.out.println
, you’ll need to refer to it by its full name, java.lang.System.out.println
.
java
and javax
are reserved by the JVM for built-in and extensions classes, but much like anyone can register a domain name on the Internet, anyone can declare a package namespace. There’s a loose convention that you should use your reverse domain name as a prefix (for example, if our department released a package for autograding, we might put it in the edu.umass.cs.autograder
package). But many modern java packages declare a top-level namespace – you’ll see this in Lab 4, where the Processing project just prefixes their namespace with “processing”. In practice, projects that do similar things don’t usually have the same name, and/or agree to avoid namespace collisions.
Importing packages
Sometimes you want to use something not in the current namespace. Then you need to “import” it.
For example, if I want to print a random number between one and six:
public class Die {
public static void main(String[] args) {
Random r = new Random();
System.out.println(r.nextInt(6) + 1);
}
}
because Random
is not in the namespace. But I can use an import statement: import java.util.Random;
to add it to the namespace.
Finding packages
Where does Java look for packages? By default the JVM has access to a set of “built-in” packages, that form the Java Platform API:
https://docs.oracle.com/javase/8/docs/api/index.html?overview-summary.html
Again, mostly in the java
and javax
namespace, but also some others.
But where do these classes, that is, the code, actually live? On my machine, a big chunk of them live in /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib
in the JARs there.
JARs are essentially ZIPfiles of compiled Java classes with some extra stuff (a Java-specific manifest, describing their contents, that the JVM knows how to read). Let’s take a look in rt.jar
. Hey look! Our friends System
and String
!
You may have noticed that the file, say String.class
(which is the compiled representation of String.java
) lives in a directory java/lang/
. That looks a lot like java.lang.
, doesn’t it?
Not a coincidence! The JVM requires that packages map to (that is, directly correspond to) directories with the same name(s), and that classes map to .class
files within those directories.
But there’s still a piece of the puzzle missing. How does the JVM know where to look for these directories? How did it know, for example, that /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/rt.jar
was a place to search?
CLASSPATH and friends
There are three mechanisms the JVM uses. One is under your control: the “CLASSPATH”. The other two you can’t (easily) change – there is a “bootstrap CLASSPATH” and an “extensions directory”.
The latter two are configured when your JVM is installed, and they contain classes that are part of the JRE, JDK platform, and vendor-distributed extensions.
But the CLASSPATH you do control. If you run from within Eclipse, you can add JARs (and directories) to the CLASSPATH by selecting the appropriate menu items, either “Build Path -> Add to Build Path” for JARs or “Build Path -> Use as Source Folder” for a directory. You can also manage the Build Path (“Configure Build Path”) and view what’s on it, which is how I found the rt.jar
I showed you earlier.
Putting your own classes into packages
If you want to put a class into a package, like, for example, you want to move our Die
class into the nerdy.gaming
package, you need to explicitly declare the package at the top of the file, and move it into an appropriate directory, in order for it to compile and be recognized by the JVM. Eclipse will prompt you to do one if you do the other.
Class exercise
Suppose I had the following directory hierarchy:
src/marc/liberatore/Banana.java
support/marc/liberatore/Smoothie.java
and the following code in Smoothie.java
:
class Smoothie {
public static void main(String[] args) {
System.out.println("Add a " + new Banana());
}
}
- What directories need to be on the classpath in order for this to compile?
- What
package
should we declare at the top of this file? - Do we need to
import
anything? If so, what?