[Access to Wang masthead]

Using Script Languages

Part 6: Java

From "VS & Beyond",  Access to Wang, June 1998
  [ Prior Article ]     [ Return to the Catalog of articles ]     [ Next Article ]  

If there can be any new technology that has been hyped more than the World Wide Web, it would have to be Java. Loudly acclaimed as either the savior of the programming world or a novel programming language with nothing special to recommend it, Java has attracted enough interest (and funding) to make it a good career choice regardless of its actual merits. Daily we hear reports of large contract awards for Java applications or strategic directions by major corporations built around Java development.

Java can be considered to be peripheral to the mainstream of scripting languages. Like Perl, Java can be used to solve problems that normally require scripts - such as screen control for Web browsers, additional features on Web pages, etc. - but it is also powerful enough to develop complex applications. It also shares Perl's transportability between platforms and reliance on run-time (interpretive) operation - though Java code must also be compiled before use.

Java differs from all other languages in that it is designed to be distributed to client systems over a network, and it includes security features that restrict its use to specific functionality and provide a means for checking received code for authenticity and completeness.

Java was developed by Sun Microsystems, who set up a new company - JavaSoft - to license Java usage and promote the use of Java. They chose to supply development tools and libraries for free, so you can download the Java Development Kit (JDK) without cost and try it out for yourself. (See the References below for details.)

Operating environments

Java applications (applets) are designed to be distributed across a network and executed within a local interpreter known as the Java Virtual Machine (JVM). While the distributed code is run within this interpreter on a client system, it must also be compiled before use; the compilation process reduces some common operators to binary tokens and attaches code to allow the client to detect whether the application has been received completely or tampered with. Interpretive environments are not new to the programming world, and tokens were used by the Wang 2200 BASIC operating systems (a.k.a. the "MVP") to reduce the size of programs and speed execution; the difference in Java is the assumption that applications would be distributed over a public, unsecured network and must have checks to ensure their authenticity.

In most instances, Java programs are designed to be distributed and used through supporting Web browsers. The instigator of this process is the <APPLET> HTML tag, which is included within the Web page file and specifies the name of the class file to be downloaded. (An example HTML file is shown in Figure 2, below.)

There are also other environments where Java code can be executed, including Web servers and on Unix systems. Java applications have been used to extend the capabilities of several popular Web servers, providing new functionality at the server side. Recently, Sun Microsystems has announced partnerships with hardware manufacturers to use native versions of Java in small computing devices and a variety of appliances.

Language model

Java's syntax borrows heavily from several languages, including C, C++, Smalltalk, Perl, Modula2, Eiffel, and others. It's structure most closely resembles C or Perl. It uses the Object Oriented (OO) model of program construction, allowing you to extend existing classes or create new ones as needed. It provides most of the flexibility and speed of C or C++, but provides its own internal system of garbage collection (reusing areas of memory that are no longer held), removing one of the primary sources of errors in C/C++ programming.

Like some of the other script languages discussed in this series (especially VBScript), Java functions are referred to using a hierarchy of properties, methods, and objects. For example, a call to the drawString method within the graphics object would be coded as graphics.drawString. Java uses a highly structured syntax where items can be shown or hidden from view (public or private), variable names can be reused in other sections, and most existing functions can be extended or used to form new functions or objects.

Java has several aspects that are unique. It is designed to be portable: versions of Java programs can be run on any platform that has a compatible Virtual Machine available. It provides extensive security features: Java applets contain code to check for completeness and authenticity (using digital signatures and certificates) and authorized implementations of the Virtual machine do not allow "dangerous" operations that might compromise the client environment, such as changes to local files, applications, or system settings.

Java's operational limits also constrain the types of applications that can be developed; the built-in limits to its scope means you cannot use Java in some system control applications. In contrast, Microsoft's ActiveX environment - in some ways similar to Java in purpose, usage, and application - provides access to vital system resources, allowing these control applications to be built (with some risk).

Development tools

Besides the Java Development Kit (JDK) listed below, there are many commercial development tools that provide a richer, more productive environment than the command-line compiler world of the JDK. Some of the best-known tools include those from Borland, Symantec, IBM, and others. Most of these are Interactive Development Environments (IDEs), which provide an integrated editor, debugger, and documentation environment. As the Java frenzy continues, more such tools appear daily.

With its Object Oriented underpinnings, standard Java class libraries can be developed and sold, freeing the developer to concentrate on their application and not on providing basic program functionality. A market is developing to supply these items, just as vendors of C++ class libraries and Visual Basic custom controls have blossomed in the past.

Example of Use

The example code (Figure 1, below) shows a small applet that places text on the screen and allows the user to drag it around. The example is taken from the book Exploring Java. Like most Java applications, this program assumes you are using a 32-bit operating system, such as Windows 95, Windows NT, Unix, or the Macintosh OS.

In addition to the Java program code, a small HTML file is shown that will load the applet, give it its boundaries, and pass the text message to be displayed. The Java source code should be placed in a file named HelloWeb2.java and compiled with the Java compiler (javac) or by using any other Java development tool. The resulting class file - HelloWeb2.class - should be located within the same directory as the HTML file.

Once the files are created and the Java source is compiled, you can run the HTML file within any Java-aware Web browser, such as Netscape Navigator or Microsoft Internet Explorer. (Due to limitations of early versions of the Java Virtual Machine, this example must be used with versions 4.x or above of Navigator or Internet Explorer.) If all goes well, you should see your text displayed near the center of the screen. You will also find that this text can be moved around within the box by dragging it with the mouse.

Conclusion

Most of us have seen Java applets used to enhance Web pages or provide small applications. In most cases, these uses have appeared to be trivial - possibly handled by other, simpler scripting approaches. In rare cases, large applications have appeared that show Java's potential as a full development tool; for example, I use a terminal emulator product that is launched from a Web browser and fully emulates the Telnet protocol, encrypting all transmissions between host and client as it is used. Other Java-developed applications will continue to appear as the language matures.

As a script language, Java's security features overcome some of its difficulty in development, making it a better choice for some scripting applications in public settings. Next month: Concluding remarks about the script series; introducing next VS subjects.

References

Exploring Java
Pat Niemeyer, Josh Peck
O'Reilly & Associates
ISBN: 1-56592-271-9

One of the best introductions to Java programming. Unlike other books, no prior C or C++ experience is assumed.

Java in a Nutshell
David Flanagan
O'Reilly & Associates
ISBN: 1-56592-262-X

Part of O'Reilly's excellent Nutshell series. A good introduction, but some experience with Object-Oriented Programming is assumed.

Java Development Kit (JDK)
http://www.javasoft.com

The source for the free compiler, class libraries, and related tools. Not suitable for heavy usage but free of cost and easily available. Requires a 32-bit operating system and around 30 megabytes of storage space in the current version (1.1.6).

Java FAQ
http://www.best.com/~pvdl/javafaq.html

Extensive list of Java resources, including latest versions, bug reports, and developer help.


Figure 1: Sample Java Application

Program Code for HelloWeb2.java

/*
 From Exploring Java, O'Reilly & Associates, 1997
 ISBN 1-56592-271-9
*/

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class HelloWeb2 extends Applet implements
MouseMotionListener {
     int messageX = 125, messageY = 95;
     String theMessage;

     public void init() {
          theMessage = getParameter("message");
          addMouseMotionListener(this);
     }

     public void paint( Graphics graphics ) {
          graphics.drawString( theMessage, messageX, messageY );
     }

     public void mouseDragged( MouseEvent e ) {
          messageX = e.getX();
          messageY = e.getY();
          repaint();
     }
     public void mouseMoved( MouseEvent e ) { }
}


Figure 2: Associated HTML Page HelloWeb2.html


<html>
<head>
</head>
<body>
     <applet code=HelloWeb2 width=300 height=200>
     <param name="message" value="(Drag me around)">
     </applet>
</body>

  [ Prior Article ]     [ Return to the Catalog of articles ]     [ Next Article ]  


Copyright © 1998 Dennis S. Barnes
Reprints of this article are permitted without notification if the source of the information is clearly identified