22.3 Using Java Classes Directly
As described in
the previous two sections, both Netscape and Internet Explorer allow
JavaScript code to interact with Java applets and Java applets to
interact with JavaScript. Netscape's
LiveConnect technology also allows JavaScript programs to instantiate
their own Java objects and use them, even in the absence of any
applets. Internet Explorer does not have any analogous capability.
In Netscape, the
Packages object provides access to all the
Java packages that Netscape knows about. The expression
Packages.java.lang refers to the
java.lang package, and the expression
Packages.java.lang.System refers to the
java.lang.System class. For convenience,
java is a shortcut for
Packages.java. In Netscape, JavaScript code might
invoke a static method of this java.lang.System
class as follows:
// Invoke the static Java method System.getProperty( )
var javaVersion = java.lang.System.getProperty("java.version");
This use of LiveConnect is not limited to system classes, because
LiveConnect allows us to use the JavaScript new
operator to create new instances of Java classes (just as we would in
Java). Example 22-2 shows JavaScript code that uses
standard Java classes (the JavaScript code looks almost identical to
Java code, in fact) to pop up a window and display some text. The
result is shown in Figure 22-1.
Figure 22-1. A Java window created from JavaScript

Example 22-2. Scripting the built-in Java classes
var f = new java.awt.Frame("Hello World");
var ta = new java.awt.TextArea("hello, world", 5, 20);
f.add("Center", ta);
f.pack( );
f.show( );
The code in Example 22-2 creates a simple Java user
interface. What is missing, however, is any form of event handling or
user interaction. A program like the one shown here is restricted to
doing output, since it doesn't include any way for JavaScript
to be notified when the user interacts with the Java window. It is
possible, though complicated, to use JavaScript to define a Java user
interface that responds to events. In Java 1.1 and later,
notification of an event is performed by invoking a method of an
EventListener object. Since Java applets can execute arbitrary
strings of JavaScript code, it is possible to define a Java class
that implements the appropriate EventListener interface and invokes a
specified string of JavaScript code when it is notified that an event
has occurred. If you create an applet with a method that allows you
to create such EventListener objects, you can use JavaScript to piece
together Java GUIs that include event handlers defined in JavaScript.
Note that LiveConnect does not give complete and unrestricted access
to the Java system; in other words, there are some things we cannot
do with LiveConnect. For example, LiveConnect does not give us the
capability to define new Java classes or subclasses from within
JavaScript, nor does it give us the ability to create Java
arrays.[2] In addition to these limitations, access to the standard
Java classes is restricted for security reasons. An
untrusted
JavaScript program cannot use the java.io.File
class, for example, because that would give it the power to read,
write, and delete files on the host system. Untrusted JavaScript code
can use Java only in the ways that untrusted applets can.
[2] JavaScript programs can create arrays
indirectly, using the Java 1.1 method
java.lang.reflect.Array.newInstance( ).