OOP in Java: Handout 1

A Short Java How-to

How to compile a class

From UNIX shell, run:
javac MyClass.java
Or, if you need to specify a classpath other than the default:
javac -classpath mypath MyClass.java
This should produce a class file MyClass.class. The javac compiler will try to (recursively) locate and compile any classes used by the compiled class. However, don't depend on it to recognize that the source (.java) and object (.class) files are out of sync for any class. Either remove all .class files before compiling, or use make. You will save yourself a lot of frustration. Note: The javac compiler is pretty insistant that the file MyClass.java contain the class MyClass.

How to run a Java program

To run a Java program, you call the Java runtime with the class containing the main() for your function. No extension is required on the class name.
java  MyClass

How to time a Java program

It is occasionally interesting to find out exactly how slow a Java program is. You can do this by runnint the UNIX time command.
time java  MyClass
This will display user, system, and total elapsed time used by the process (in a more or less unreadable format).

How to specify a classpath

This is somewhat system specific but in UNIX it is generally a list of directories, and/or .jar files separated by ':'. (If ':' doesn't work, try ';' and remember to enclose in "" to avoid the semicolon from being interpreted by the shell.)
javac -classpath "foo:.:/user/me/classes/:libdir/goodstuff.jar"  MyClass
Note that .jar files, being archives, look like paths to javac.

How to ...

Submit question and we will add answers.

Useful Resources

Java 1.2 API: Beautiful documentation for many Java classes and interfaces from the class libraries. You can also get the Java 1.3 documentation as well.

Java 1.2 Doc Homepage: A list of links to Java documentation.

Java 1.3 Doc Homepage: A list of links to Java documentation.

Javadoc Homepage: A list of links to documentation on Javadoc.

java.sun.com: Java home page at Sun. You can find lots of stuff from here (eg servlet API documentation), but it's harder than you'd think.

Google: An excellent resource. If you are stuck, type in your question (or it's keywords) and go. I find it the best question answering system and best overall resource on the Web. To find information on some programming structure, problem or utility, try searching on "mytopic tutorial" or "mytopic reference". Ex. Searching on "java threads tutorial" will find a lot useful information on how to use threads in Java.

Some documentation on GNU make: Tedious, but useful.

Bruce Eckel's MindView inc page Bruve Eckel is the author of Thinking in Java, Thinking in Patterns, and several other books. The Thinking in Java book is a good complement to the Core Java book used in class. TIJ2 is available from free in HTML or PDF from this site (which is a good reason in itself to go buy it). [Note: I consider TIJ2 and Core Java to be the two best Java book out there. I think the Core Java is a better reference and overview (which is why I chose it for the course), while the TIJ is probably a better beginning tutorial and OOP manifesto.]