Java Tutorial Appendices
This tutorial appendix covers material (eg. reference material and code & application distribution) that is not included in the tutorials.
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, 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() |
Mouse movements |
| MouseWheelListener * | mouseWheelMoved() | Mouse wheel motion |
| TextListener | textValueChanged() | Text content changes |
| WindowListener * | windowActivated() windowClosed() windowClosing() windowDeactivated(), 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 and programmers to use these classes as easily as using those distributed in 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.6.0_24\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 |
To unpackage a .jar file you use the commands:
- java -jar file_name.jar
- jar -x file_name.jar. If you just want to view the contents first, use the -tf option instead of -x.
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.
Builder tools provide ways to create Beans, 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 checks for state and behaviour. NetBeans IDE provides a builder tool and a quickstart tutorial.
Oracle provides a complete tutorial on creating and using Java Beans.
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 JDK 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.
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!
Global Hotkeys
Global hotkeys allow a program to be activated from any other program. Java uses its events handler system to monitor the global hotkeys registry after a key sequence has been entered in the registry. Check out JIntellitype (Wintel) and JxGrabKey (Linux) or google 'global hotkeys in java'.