Jar Files: Java 3D™ Javamail™
Jar Files: Java 3D™ Javamail™
Jar Files: Java 3D™ Javamail™
archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications. The JAR file format provides many benefits:
Security: You can digitally sign the contents of a JAR file. Users who recognize your signature can then optionally grant your software security privileges it wouldn't otherwise have. Decreased download time: If your applet is bundled in a JAR file, the applet's class files and associated resources can be downloaded to a browser in a single HTTP transaction without the need for opening a new connection for each file. Compression: The JAR format allows you to compress your files for efficient storage. Packaging for extensions: The extensions framework provides a means by which you can add functionality to the Java core platform, and the JAR file format defines the packaging for extensions. Java 3D and JavaMail are examples of extensions developed by SunTM. By using the JAR file format, you can turn your software into extensions as well. Package Sealing: Packages stored in JAR files can be optionally sealed so that the package can enforce version consistency. Sealing a package within a JAR file means that all classes defined in that package must be found in the same JAR file. Package Versioning: A JAR file can hold data about the files it contains, such as vendor and version information. Portability: The mechanism for handling JAR files is a standard part of the Java platform's core API.
In computing software, a JAR file (or Java ARchive) aggregates many files into one.[1] Software developers generally use .jar files to distribute Java applications or libraries, in the form of Java class files and associated metadata and resources (text, images, etc.). JAR files build on the ZIP file format. Computer users can create or extract JAR files using the jar command that comes with a JDK. They can also use zip tools to do so; however, caution should be exercised as to the order of entries in the zip file headers as the manifest likely needs to be first. The package java.util.zip contains classes that read and write JAR files. The org.apache.tools.zip package is released under the Apache Software Foundation license. Several related file formats build on the JAR format:
WAR (Web Application aRchive) files, also Java archives, store XML files, Java classes, JavaServer Pages and other objects for Web Applications.
RAR (Resource Adapter aRchive) files (not to be confused with the RAR file format), also Java archives, store XML files, Java classes and other objects for J2EE Connector Architecture (JCA) applications. EAR (Enterprise ARchive) files provide composite Java archives which combine XML files, Java classes and other objects including JAR, WAR and RAR Java archive files for Enterprise Applications. SAR (Service ARchive) is similar to EAR. It provides a service.xml file and accompanying JAR files.
A JAR file has an optional manifest file located in the path METAINF/MANIFEST.MF. The entries in the manifest file determine how one can use the JAR file. JAR files intended to be executed as standalone programs will have one of their classes specified as the "main" class. The manifest file would have an entry such as Main-Class: myPrograms.MyClass Users can typically start such JAR files with a command similar to: java -jar foo.jar To unzip a JAR file users can use any standard unzip software. However, anyone with an installed Java Virtual Machine can use the jar command to expand the file: jar -xf foo.jar These files can also include a Classpath entry, which identifies other JAR files for loading with the JAR. This entry consists of a list of absolute or relative paths to other JAR files. Although intended to simplify JAR use, in practice it turns out to be notoriously brittle, as it depends on all the relevant JARs being in the exact locations specified when the entry-point JAR was built. To change versions or locations of libraries, a new manifest is needed.
elements are used. A TextEvent object is generated when text of a component is modified. An ItemEvent is generated when an item from a list, a choice or checkbox is selected. Low-Level Events is one that represents a low-level input or windows-system occurrence on a visual component on the screen. The various low-level event classes and what they generate are as follows: A ContainerEvent Object is generated when component are added or removed from container. A ComponentEvent object is generated when a component is resized, moved etc. A FocusEvent object is generated when component receives focus for input. A KeyEvent object is generated when key on keyboard is pressed, released etc. A WindowEvent object is generated when a window activity, like maximizing or close occurs A MouseEvent object is generated when a mouse is used. A PaintEvent object is generated when component is painted. Event Listeners: An object delegates(entrust, assign, transfer.) the task of handling an event to an event listener. When an event occurs, an event object of the appropriate type (as illustrated below) is created. This object is passed to a Listener. A listener must implement the interface that has the method for event handling. A component can have multiple listeners, and a listener can be removed using removeActionListener () method. Next question in your mind must be what is an interface?. An Interface contains constant values and method declaration. The difference between classes and interface is that the methods in an interface are only declared and not implemented, that is, the methods do not have a body. What is the Need for interface? Are interfaces are used to define behavior protocols (standard behavior) that can be implemented by any class anywhere in the class hierarchy. The java.awt.event package contains definitions of all event classes and listener interface. The semantic listener interfaces define by AWT for the above mentioned semantic events are: ActionListener AjdustmentListener
ItemListener TextListener The low-level event listeners are as follows: ComponentListener ContainerListener FocusListener KeyListener MouseListener MouseMotionListener WindowsListener. How does the above Application work? The execution begins with the main method. An Object of the MyEvent class is created in the main method. Constructor of the MyEvent class is invoked. Super () method calls the constructor of the base class and sets the title of the window as given. A button object is created and placed at the center of the window. A Listener Object is created. The addActionListener() method registers the listener object for the button. SetVisible () method displays the window. The Application waits for the user to interact with it. When the user clicks on the button labeled Click Me: The ActionEvent event is generated. Then the ActionEvent object is created and delegated to the registered listener object for processing. The Listener object contains the actionPerformed() method which processes the ActionEvent In the actionPerformed() method, the reference to the event
source is retrieved using getSource() method. The label of the button is changed to Button has been clicked, Guru! using setText() method. AWT
The Java programming language class library provides a user interface toolkit called the Abstract Windowing Toolkit, or the AWT. The AWT is both powerful and flexible.
The Abstract Window Toolkit (AWT) is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes (JFC) the standard API for providing a graphical user interface (GUI) for a Java program. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT. The AWT provides two levels of APIs:
A general interface between Java and the native system, used for windowing, events, layout managers. This API is at the core of Java GUI programming and is also used by Swing and Java 2D. It contains: o The interface between the native windowing system and the Java application; o The core of the GUI event subsystem; o Several layout managers; o The interface to input devices such as mouse and keyboard; and o A java.awt.datatransfer package for use with the Clipboard and Drag and Drop. A basic set of GUI widgets such as buttons, text boxes, and menus. It also provides the AWT Native Interface, which enables rendering libraries compiled to native code to draw directly to an AWT Canvas object drawing surface.
AWT also makes some higher level functionality available to applications, such as:
Access to the system tray on supporting systems; and The ability to launch some desktop applications such as web browsers and email clients from a Java application.
Neither AWT nor Swing are inherently thread safe. Therefore, code that updates the GUI or processes events should execute on the Event dispatching thread. Failure to do so may result in a deadlock or race condition. To address this problem, a utility class called SwingWorker allows applications to perform time-consuming tasks following userinteraction events in the event dispatching thread.
Figure 3 shows the inheritance relationship between the user interface component classes provided by the AWT. Class Component defines the interface to which all components must adhere.
The AWT provides nine basic non-container component classes from which a user interface may be constructed. (Of course, new component classes may be derived from any of these or from class Component itself.) These nine classes are class Button, Canvas, Checkbox, Choice, Label, List, Scrollbar, TextArea, and TextField. Figure 4 depicts an instance of each class.
Types of containers
The AWT provides four container classes. They are class Window and its two subtypes -class Frame and class Dialog -- as well as the Panel class. In addition to the containers provided by the AWT, the Applet class is a container -- it is a subtype of the Panel class and can therefore hold components. Brief descriptions of each container class provided by the AWT are provided below.
Window Frame
A top-level display surface (a window). An instance of the Window class is not attached to nor embedded within another container. An instance of the Window class has no border and no title. A top-level display surface (a window) with a border and title. An instance of the Frame class may have a menu bar. It is otherwise very much like an instance of the Window class.
Dialog Panel
A top-level display surface (a window) with a border and title. An instance of the Dialog class cannot exist without an associated instance of the Frame class. A generic container for holding components. An instance of the Panel class provides a container to which to add components.
Creating a container
Before adding the components that make up a user interface, the programmer must create a container. When building an application, the programmer must first create an instance of class Window or class Frame. When building an applet, a frame (the browser window) already exists. Since the Applet class is a subtype of the Panel class, the programmer can add the components to the instance of the Applet class itself. The code in Listing 1 creates an empty frame. The title of the frame ("Example 1") is set in the call to the constructor. A frame is initially invisible and must be made visible by invoking its show() method. import java.awt.*;
public class Example1 { public static void main(String [] args) { Frame f = new Frame("Example 1"); f.show(); } }
The code in Listing 2 extends the code from Listing 1 so that the new class inherits from class Panel. In the main() method, an instance of this new class is created and added to the Frame object via a call to the add() method. The result is then displayed. The results of both examples should look identical (that is, they should look quite uninteresting). import java.awt.*;
public class Example1a extends Panel { public static void main(String [] args) { Frame f = new Frame("Example 1a");
Listing 2. A frame with an empty panel By deriving the new class from class Applet instead of class Panel, this example can now run as either a standalone application or as an applet embedded in a Web page. The code for this example is provided in Listing 3. The resulting applet is displayed in Figure 5 (and is still quite uninteresting).
import java.awt.*; public class Example1b extends java.applet.Applet { public static void main(String [] args) { Frame f = new Frame("Example 1b"); Example1b ex = new Example1b(); f.add("Center", ex); f.pack(); f.show(); } }
Note: a Window object, and in certain cases even a Dialog object, could replace the Frame object. They are all valid containers, and components are added to each in the same fashion.
Adding components to a container
To be useful, a user interface must consist of more than just a container -- it must contain components. Components are added to containers via a container's add() method. There are three basic forms of the add() method. The method to use depends on the container's layout manager (see the section titled Component layout). The code in Listing 4 adds the creation of two buttons to the code presented in Listing 3. The creation is performed in the init() method because it is automatically called during applet initialization. Therefore, no matter how the program is started, the buttons are created, because init() is called by either the browser or by the main() method. Figure 6 contains the resulting applet. import java.awt.*;
public class Example3 extends java.applet.Applet { public void init() { add(new Button("One")); add(new Button("Two")); } public Dimension preferredSize() { return new Dimension(200, 100); } public static void main(String [] args) { Frame f = new Frame("Example 3"); Example3 ex = new Example3(); ex.init(); f.add("Center", ex); f.pack(); f.show(); } }
The AWT provides five layout managers. They range from very simple to very complex. This article covers only the two layout manager classes used by the examples herein: the FlowLayout class and the BorderLayout class. The FlowLayout class places components in a container from left to right. When the space in one row is exhausted, another row is started. The single-argument version of a container's add() method is used to add components. The BorderLayout class has five zones as depicted in Figure 7. The zones are named "North", "South", "East", "West", and "Center". A single component can be placed in each of these five zones. When the enclosing container is resized, each border zone is resized just enough to hold the component placed within. Any excess space is given to the center zone. The two-argument version of a container's add() method is used to add components. The first argument is a String object that names the zone in which to place the component.
Each container class has a default layout manager. The default layout manager for the Frame class and Dialog class is the BorderLayout manager. The default layout manager for the Panel class (and the Applet class) is the FlowLayout manager. The code in Listing 5 uses both layout managers and includes a few more user interface components. The result is displayed in Figure 8.
import java.awt.*; public class Example4 extends java.applet.Applet { public void init() { Panel p; setLayout(new BorderLayout()); p = new Panel(); p.add(new TextArea());
add("Center", p); p = new Panel(); p.add(new Button("One")); p.add(new Button("Two")); Choice c = new Choice(); c.addItem("one"); c.addItem("two"); c.addItem("three"); p.add(c); add("South", p); } public static void main(String [] args) { Frame f = new Frame("Example 4"); Example4 ex = new Example4(); ex.init(); f.add("Center", ex); f.pack(); f.show(); } }
Event handling
The examples above do nothing more than display an inert user interface. It is, of course, very important that a user interface take action as a result of user input. It is, however, beyond the scope this article to delve deeply into the mysteries of event handling. That must
wait until a future article. However, in the interest of completeness, the example code in Listing 6 shows how to handle one type of event a program may receive. The new class overrides the action() method provided by the Component class. The action() method responds to the action events that are generated, for example, by the selection of an item from a pop-up list. The action() method requires that two parameters be supplied, an Event instance and an Object instance. The Event instance contains information about the event, including the target of the event (the component that first received the event), the x and y coordinates of the event, and the time when the event occurred. The Object instance holds an event-specific piece of data. For Button objects it contains the text in the button label. import java.awt.*;
public class Example5 extends java.applet.Applet { TextArea ta = null; public void init() { Panel p; setLayout(new BorderLayout()); p = new Panel(); ta = new TextArea(); p.add(ta); add("Center", p); p = new Panel(); p.add(new Button("One")); p.add(new Button("Two")); Choice c = new Choice(); c.addItem("one"); c.addItem("two"); c.addItem("three"); p.add(c);
add("South", p); } public boolean action(Event e, Object o) { String str = (String)o; ta.appendText(str + "\n"); return false; } public static void main(String [] args) { Frame f = new Frame("Example 5"); Example5 ex = new Example5(); ex.init(); f.add("Center", ex); f.pack(); f.show(); } }
Programs written in Java are called applets. The first browser that could show applets was introduced in 1994, as "WebRunner" - later known as "The HotJava Browser".