I. Running Java A. Getting it ------------------------------------------ GETTING JAVA Already on department Linux machines, in /opt/java/j2sdk1.4.1_01/ For home, go to http://java.sun.com and get J2SDK 1.4.1_01 What you get: javac - compiler java - interpreter (virtual mach.) jar - archiver javadoc - HTML page creation ... ------------------------------------------ B. Hello World ------------------------------------------ SUMMARY OF COMILATION AND RUNNING HelloWorld.java | | javac HelloWorld.java v HelloWorld.class | | java HelloWorld v (output) ------------------------------------------ C. setting your CLASSPATH Who can explain what the PATH does in Unix? ------------------------------------------ THE CLASSPATH (ON LINUX) On Linux, cygwin $ printenv CLASSPATH .:... $ java HelloWorld What if "." is not in the CLASSPATH? $ export CLASSPATH="" $ java HelloWorld Using multiple directories: $ ls applogic userinterface $ cd applogic $ export \ CLASSPATH="$HOME/classes/cs362/lectures/objects-classes/applogic" $ javac Message.java $ export \ CLASSPATH=".:$HOME/classes/cs362/lectures/objects-classes/applogic" $ javac Message.java $ cd ../userinterface $ javac HelloWorld.java $ export \ CLASSPATH="$HOME/classes/cs362/lectures/objects-classes/applogic" $ javac HelloWorld.java HelloWorld.java:6: cannot resolve symbol ... ------------------------------------------ So can you summarize how the CLASSPATH works so far? D. packages in Java ------------------------------------------ PACKAGES Goals: prevent name conflicts support large scale development For each name N in package P can refer to N by P.N from outside package Packages contain: classes, interfaces, packages Example: userinterface HelloWorld applogic Message ------------------------------------------ ------------------------------------------ EXAMPLE // Add the following line of code // to put this class in package applogic. package applogic; /** This class knows the message to print. */ public class Message { /** the message */ private String msg; /** Initialize this Message object. */ public Message() { msg = "Hello, class!"; } /** Return the message. */ public String get() { return msg; } } ------------------------------------------ What do we have to do to make the code in HelloWorld work with this? Can you summarize how java and javac use CLASSPATH? E. Eclipse ------------------------------------------ ECLIPSE EDITOR Free, IDE for Java Get it from: http://eclipse.org Already installed on Linux: $ eclipse ------------------------------------------ II. Exception Handling Have you used exceptions in C++? A. motivation ------------------------------------------ MOTIVATION FOR EXCEPTION HANDLING How to make a "bullet-proof" abstraction? Returning a status code: - inefficient in the normal case - users who forget aren't warned ==> insecure - checking status gets in the way ==> hard to read programs Exit the program if problems found: - inflexible ==> poor reuse Require callers to check preconditions: - doesn't work for untrusted clients ==> insecure - hard for clients to check sometimes ==> inefficient, hard to read Better: exceptions, try/catch statements ------------------------------------------ So when should you use exceptions in a design? B. exception mechanism 1. terms ------------------------------------------ EXCEPTION HANDLING MECHANISMS A convenient form of passing blocks Terms: An exception is an object that may be The code that is executed in response ------------------------------------------ C. in Java 1. exception hierarchy ------------------------------------------ EXCEPTION TYPES IN JAVA All exceptions are subclasses of Throwable Unchecked exceptions are subclasses of either Error or RuntimeException Throwable Error AWTError LinkageError ThreadDepthError VirtualMachineError OutOfMemoryError ... Exception AWT Exception ClassNotFoundException CloneNotSupportedException IOException EOFException FileNotFoundException ... RuntimeException ArithmeticException ArrayStoreException ClassCastException SecurityException IndexOutOfBoundsException NullPointerException IllegalArgumentException ... ------------------------------------------ Advantages of a hierarchy? 2. declarations in Java of what exceptions a method can throw ------------------------------------------ DECLARING EXCEPTIONS THROWN BY METHODS IN JAVA Syntax for declaring exceptions that may be thrown by a method: throws [, ] ... Example: char readChar() throws IOException { ...readByte() ... } byte readByte() throws IOException { ... } ------------------------------------------ Why? 3. throwing exceptions ------------------------------------------ THROWING AN EXCEPTION To throw an exception you have to - create an exception object, - use "throw" to throw it. Syntax: throw ; Example: throw new IOException("bad file"); ------------------------------------------ 4. catching exceptions ------------------------------------------ CATCHING AN EXCEPTION Syntax: try [catch ( ) ] ... [finally ] Example: try { } catch (IOException ioe) { System.err.println(e.getMessage()); } catch (NullPointerException npe) { System.err.println(npe); throw npe; } finally { myFile.close(); } ------------------------------------------ What else needs to be said?