Chapter 31
LiveConnect—Connecting JavaScript, Java, and Plug-ins
What is LiveConnect?
LiveConnect enables communication between JavaScript and Java applets in a page, and
between JavaScript and plug-ins loaded on a page. Originally named JavaScript Wiring at
the March 1996 Netscape Developer Conference, this technology has been recently renamed
LiveConnect. With LiveConnect you can:
- Use JavaScript to access Java variables, methods, classes, and packages directly.
- Control Java applets or plug-ins with JavaScript.
- Use Java code to access JavaScript methods and properties.
Enabling LiveConnect
LiveConnect is enabled by default in Netscape Navigator 3.0x and 4.0. Because
LiveConnect enables communication between JavaScript and Java, both these languages should
be enabled. To confirm that LiveConnect is enabled, choose Network Preferences from
the Options menu, then choose the Languages tab. Make sure that both check boxes
are checked. Note that even if you plan to use LiveConnect to control plug-ins with
JavaScript, the Enable Java box must be checked.
LiveConnect doesn’t work on Netscape Navigator 3.0x for Mac 68K or Windows 3.1x
operating systems.
The Java Console
The Java Console is a Navigator window that displays Java messages. When you use the
class variables out or err in java.lang.System to output a
message, the message appears in the Java Console. For example, the following init()
method of a Java class displays the message “Class loaded!” in the Java
Console:
public void init() {
System.out.println("Class loaded!");
}
Figure 31-1 shows the Java Console after loading an applet consisting of the preceding init()
method.
Figure
31-1. The Java Console after printing a short message in it.
You can use the Java Console to present messages to the user or to trace the values of
variables at different stages of an applet’s execution. However, do not rely on it for
presenting messages to the user because the Java Console is rarely open.
Load the Java Console in Netscape Navigator 3.0x from the Options menu. In Netscape
Navigator 4.0, this feature is available in the Window menu.
Netscape Packages
LiveConnect-enabled versions of Netscape Navigator come with a file that includes the
following Java packages:
- netscape packages to enable JavaScript and Java communication.
- java and sun packages to provide security enhancements for
LiveConnect.
In Netscape Navigator 3.0x this file is named java_30 (or java_301 for Navigator 3.01),
whereas in Navigator 4.0 it is named java_40.
If you have some Java programming experience, you probably know that the java
and sun packages already exist in the Sun 1.0.2 Java Development Kit (JDK). These
packages replace packages in the JDK src.zip archive.
The file java_x0 contains the following packages:
- netscape.javascript implements the JSObject class to let your Java
applet access JavaScript methods and properties. It also implements JSException
to throw an exception when JavaScript code returns an error.
- netscape.plugin implements the Plugin class to enable JavaScript and
plug-in communication. Compile your plug-in with this class to allow applets and
JavaScript code to manipulate the plug-in.
In addition, java_x0 contains some other netscape packages: netscape.applet
is a replacement for the Sun JDK package sun.applet, and netscape.net is
a replacement for the Sun JDK package sun.net.
Compiling Java Code with
Netscape Packages
The java_x0 file is delivered in the Program\java\classes directory beneath the
Navigator directory. To access the packages in java_x0, place the file in the classpath
of the JDK compiler in either of the following ways:
- Create a CLASSPATH environment variable to specify the paths and names of java_x0
and classes.zip.
- When you compile the Java code, specify the location of java_x0 and classes.zip
by using the -classpath command line parameter.
If you are using an application such as Visual J++, you should specify the path in that
application. For example, choose the Settings…option from the Build menu in
Visual J++ 1.0 to specify the directories containing the classes.
For some reasons, the internal structure of java_30 is not as expected by the Java
compiler. Therefore, you cannot use the following statement:
import netscape.javascript.*
Instead, you must specify the actual names (e.g., netscape.javascript.JSObject).
One workaround is to rename the java_30 file to java_30.zip (by adding a .zip extension).
Then, create a new directory, and unzip the archive to that directory. Specify the
directory in the CLASSPATH environment variable, the -classpath
parameter, or the corresponding field in Visual J++. Note that all applets in this chapter
were built with Visual J++ (it shouldn’t matter much).
Java to JavaScript Communication
To access JavaScript methods, properties, and data structures from your Java applet,
import the Netscape javascript package in the following form:
import netscape.javascript.*
If this technique does not work properly, try the following format:
import netscape.javascript.JSObject
import netscape.javascript.JSException
The package netscape.javascript defines the JSObject class and the JSException
exception object.
Not all applets have permission to access JavaScript. You may permit such access by
specifying the MAYSCRIPT attribute of the APPLET tag. Accessing
JavaScript when MAYSCRIPT is not present results in an exception. The following
HTML code shows how to grant a Java applet permission to access JavaScript:
<APPLET CODE="className" NAME="appletName"
HEIGHT=height WIDTH=width MAYSCRIPT>