Java Tutorial Appendices
This tutorial appendix covers reference material that is not included in the actual tutorials. Some background material such as distribution and documenting with JavaDoc are also included.
Reserved Words
Reserved words are words that can't be used as identifiers. Many of them are keywords that have a special purpose in Java.
abstract boolean break byte byvalue case cast catch char class const continue default do double else extends final finally float for future generic goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient try var void volatile while
Event Listeners
| Interface | Method(s) | Example |
|---|---|---|
| ActionListener | actionPerformed() | Button clicks |
| AdjustmentListener | adjustmentValueChanged() | Scrollbar moves |
| ChangeListener | stateChanged() | Slider moves |
| ComponentListener * | componentResized() etc. | Move, resize, hide, or show changes |
| ContainerListener * | componentAdded() componentRemoved() |
Component added or removed |
| FocusListener * | focusGained() focusLost() |
Textfield gains/loses focus |
| HierarchyBoundsListener * | ancestorMoved() ancestorResized() |
Ancestor object resized or moved |
| ItemListener | itemStateChanged() | Checkbox changes |
| KeyListener * | keyPressed() keyReleased() keyTyped() |
Text entered from keyboard |
| MouseListener * | mouseClicked() etc. | Mouse clicks |
| MouseMotionListener * | mouseDragged() mouseMoved() |
All mouse movements |
| TextListener | textValueChanged() | Text content changes |
| WindowListener * | windowActivated() windowClosed(), etc. |
Window obtains focus, closes, etc. |
Note: Listener interfaces must be implemented by either the class or innerclass that uses them. All interface methods must be implemented. * indicates events that also have adapters which do not require all associated methods to be implemented.
Packages
One of the goals of Java is code reusability. The library keeping function for code reuse is enabled through the use of packages of precompiled classes that can be imported into other projects. This allows developers to distribute their work as toolkits without distributing the source code. Programmers can use these classes as easily as using the classes distributed as part of the developers kit.
All that is required to package a group of classes for reuse in other projects or for distribution is to include the package directive in your source file prior to any executable code. An example might be: package toolkit; where toolkit refers to the folder that the class(es) will be stored in. Sublevels of folder are supported through dot notation. The convention for package names which ensures uniqueness is to reverse the order of your domain name. For example ACME.com would set up its toolkit as com.acme.toolkit.
The import directive is used to incorporate packaged classes within a new project. It is placed prior to any executable code (but after any package directive if there is one). For the above examples of packages, the corresponding import statements would be
import toolkit.*; import com.acme.toolkit.*;
The package location's root is determined by an operating system environment variable called classpath. It can have multiple entries which are used in sequence as search paths. The first matching entry is used. As an example of a classpath that checks the current directory and if the file isn't there checks the bin folder of the installed distribution package:
classpath=.;C:\Program Files\Java\jdk1.5.0_13\bin;
JAR Resources
Java Archive [JAR] files allows compression and packaging of sets of classes for distribution. They also permit the use of digital signatures for trusted components. Manifest files are used to summarize the package and indicate any Java Bean present in the package. The JAR utility is used to generate a JAR file. Its syntax is: jar options files
| Jar Command options | |
|---|---|
| Option | Description |
| c | Create a new archive |
| C | Change directories during command execution |
| f | First element of the file list is the archive name |
| m | Second element is the external manifest file name |
| M | Manifest file is to be suppressed |
| t | Table of contents |
| u | Update existing JAR file |
| v | Verbose output wanted as utility executes |
| x | Extract files from archive. Extra filenames restrict extraction |
| 0 | Zero compression wanted |
Java Beans
Java Beans are precompiled software components (ie building blocks) that have been designed to be reusable in a variety of different environments. All properties, events, and methods of a Bean that are exposed, can be controlled. Beans can be localized for global usage. Beans can exchange events with other Beans. Bean designers write classes conforming to a specific convention. Program writers use beans by configuring them with a visual builder tool (VBT).
The Bean Developer Kit [BDK] is an example of a application builder tool. Application builder tools provide palettes of available Beans, worksheets for laying Beans in a GUI, editors to configure Beans, and commands to interconnect Beans to form an application, and check on state and behaviour. BDK is available at Sun's Javabeans site.
Java Web Start
Java Web Start allows application deployment using a web server and digitally signed JAR files. All Web Start applications must be digitally signed for security reasons. Commercial releases will require an authorization from an external authority (read that as $$$). For testing purposes a temporary 90-day 'untrusted' digital signature can be created using SDK tools.
Java Web Start automatically checks the client for the correct interpreter version and downloads it if needed. Downloaded class files are cached for reuse. Unfortunately a chicken/egg situation means the user must manually download the first interpreter. To use Web Start:
- Package the application's classes with jar -cf namer.jar *.class
- Create a digital signature using: keytool -genkey -keypass swordfish -alias examplekey
- Attach the digital signature to the application using: jarsigner -keypass swordfish namer.jar examplekey
- Prepare a 64x64 gif with an appropriate icon
- Prepare a Java Network Launching Protocol (.jnlp) similar to
<?xml version="1.0" encoding="utf-8"?> <!-- JNLP File for Namer Application --> <jnlp href="namer.jnlp" codebase="http://home.cogeco.ca/~ve3ll/deploy"> <information><title>Namer Application</title> <vendor>Niagara Learning Systems</vendor> <homepage href="http://home.cogeco.ca/~ve3ll/"/> <icon href="namer.gif"/><offline-allowed/></information> <resources><j2se version="1.4+"/> <jar href="namer.jar"/></resources> <security><all-permissions/></security> <application-desc main-class="namer"/></jnlp>
- Upload the appropriate .jar, .gif and .jnlp files to your web server.
- Add a reference hook to the .jnlp file in your regular site pages.
- Add a reference hook to fetch the JRE if not present.
| Accept Untrusted Signature - Click on Start | |
| Desktop Integration (ie. shortcut) - Click on YES |
WARNING: Your server may not be configured to support the .jnlp protocol. You may be able to add a mimetype to your .htaccess file or get your server owner to reconfigure. This restriction may force an alternative solution or a change in ISP.
CAUTION: Users expect a GUI approach to their utilities. Non-GUI programs are best packaged and distributed as zip files.
Installers and AOT
An alternate solution to the Java Web Start deployment method is an installer utility which packages all required components including the interpreter into a single self-installing exe file. Batch files that alter the host system are often included. Finally batch files to clean up afterwards should be part of the packaging product.
| Java Specific: jexePack | install4j | IzPack |
| Generic: Inno Installer | Scriptable Install System | InstallShield | InstallAnyWhere |
A more secure method of distribution that violates the spirit of platform-independence is Ahead-Of-Time (AOT) compilation of Java source into native code for a specific machine. One example is the GNU project Java compiler GCJ. A commercial AOT product for the Wintel/Linux market is Jet.
Using Javadoc
Java includes a documenting utility called javadoc and a standard javadoc style to ensure a well documented program. To prepare source files for javadoc, first make your documenting comments into the form of /** comment */ (or **/ for symmetry). The keywords or document meta tags for javadoc (in order of use) are: @param, @return, @exception (or @throws), @author, @version, @see, @since, @serial, @serialData, @serialField, @deprecated and [@link]. These are followed by an appropriate description. Document meta tags can be used for classes, interfaces, fields, constructors and methods. They must immediately precede the item being documented. Each type of comment will be included in all appropriate places in the javadoc documentation notes.
| QUIRK #1: Although not required by Java, the package directive must precede any javadoc comment. |
| QUIRK #2: javadoc does not recognize new lines. If you need
them, you must force them with the HTML <br/> element. |
| QUIRK #3: Do not use HTML heading elements! They
interfere with the look of javadoc notes. Any other HTML element is ok. |
To generate the javadoc documentation first create a Docs subfolder in your project folder. Next from a DOS window run the command javadoc -d docs -author *.java. This assumes that you have correctly set the path to the java /bin folder.
To view your javadoc html documentation open the Docs folder and click on index.html.
JSP & Servlets
Java server pages (JSP) allow the creation of dynamic html documents. Servlets are server side applets. Each require an appropriate host server. To Be Developed!