"Linux Gazette...making Linux just a little more fun!"

Java and Linux

By Shay Rojansky

Not long ago, Javasoft celebrated Java's third birthday. Java, once seen as merely another way to animate and spice up web pages, has become much more than that. Nowadays, well-known software corporations have pledged their support to Java, and new Java APIs are being defined in record time. The Java technology enables programmers to finally write truly multi-platform programs, offers an advanced cross-platform GUI toolkit, embedded threading in the language and much more.

At the same time, we are seeing remarkable events in the computer software world. Microsoft, the behemoth of the industry, is being seriously threatened by anti-trust action from both the Justice Department and 20 different states. Netscape has released the source code for Communicator and may be the first company to break free of Open Software prejudice. This has attracted much interest in Open Software from companies who have traditionally feared the concept.

What do all of these events mean for the Linux operating system? It means we have a window of opportunity. Never before has the time been so right. On one hand, the industry is seriously taking a look at Linux as an open (and free) OS. Hey, if Netscape is doing it with their browser, why not an Open OS? On the other hand, Java technology offers a machine-independant way to write applications, and much of the industry has rallied behind it.

Java and the Linux Community

The Linux community itself, however, has always treated Java with an ambivalent attitude. The language that has promised to topple the hegemony of Microsoft, a dream like that of any Linux enthusiast, hasn't been accepted into the mainstream of Linux development. There are several reasons for this.

First and foremost, Java is a proprietary language owned by Sun Microsystems. This means that Sun controls every aspect of the language, the APIs and their licensing conditions. Tactics by Microsoft, like changing APIs in their Java suite J++ and rendering their virtual machine incompatible with other Java virtual machines, have forced Sun to seek exclusive rights to dub a product ``Java-compatible''. Although this may be the only way to fight Microsoft's unfair tactics, never before has a language been so much in the hands of a single corporation. The Linux community was born much in protest of this kind of ownership.

Second, the multi-platform concept of Java, the Java Virtual Machine (JVM), means that programmers feel they are programming for the Java environment and not for the Linux environment. This also means that it's much more difficult to exploit the features of Linux.

Third, Java is still slow. Many promising enhancements are available such as Just-in-Time compilers and Sun's Hotspot (still in beta). Java has certainly improved since it was first created, but it still requires a powerful platform. The Linux world is relatively speed-minded, and one of the main advantages of Linux is its ability to run on obsolete hardware.

The Advantages of Programming in Java

Despite all these shortcomings in the nature of Java, it is the only real challenge made in the last few years to Microsoft's rule. It is also an advanced language, written from the ground up with modern programming concepts in mind; all the flaws C++ retained from C for backwards compatibility are gone in Java, along with other complex features (multiple inheritance, for example). An automatic garbage collector removes the need to free memory, drastically reducing development time. Threads are so embedded in the language they become an indispensible tool for the programmer.

I hope Linux developers take a second look at Java as a development language and start using it regularly. Some Linux developers have already made impressive progress with Java tools, including several Java virtual machines (JVMs), several Just-In-Time (JIT) compilers and others. Take a look at these if you are considering using your Linux platform for developing Java. The Java-Linux resources page can be found at: http://www.blackdown.org/java-linux.html

I will now go over some of the key features in JDK 1.1.x. Note that the next version, 1.2 is in beta but should be available soon.

Object Serialization

Object serializing means taking an object and flattening it into a stream of bytes. In practice, this is usually used for two things--passing objects through a network and storing objects in files. Usually, a programmer who wishes to store a data structure on disk has to write a specific algorithm for doing so, which can be quite tedious. Java simplifies all this by doing it automatically for you. For example, if you have a tree in memory and wish to pass it to another Java program on the network, all you have to do is to pass the root object--Java will follow the pointers and copy the entire tree. If you have special considerations (like security), you may design the way the object is serialized.

Java Foundation Classes (Swing)

The original AWT, which is the windowing toolkit for Java, was very clunky and uncomfortable. Many components were missing and the programming model was needlessly painful. The current accepted toolkit for Java is code-named Swing. Swing offers a large number of lightweight components; they are fully implemented in Java but do not use the underlying windowing architecure as in AWT. This assures the same functionality across platforms. Another appealing feature is the completely pluggable look and feel, which lets you switch between Windows and Motif, for example, while the program is running. You can also design your own look.

RMI (Remote Method Invocation)

RMI is the Java equivalent of CORBA, which is a way to invoke methods in objects that are in a different JVM (or even machine). For those of you who know the RPC (Remote Procedure Call) frequently used in UNIX machines, RMI (and CORBA) are its object-oriented counterparts. The concept of ``distributed programming'' has gotten very popular lately. In general, it means a very tight integration between programs across the network; objects in different machines can talk to each other simply by calling each other's methods. This is accomplished by having a Java program hold a ``stub'' of a remote object. Then, when a method is invoked on that stub, Java transparently sends the request over the network and returns the requested value. The extent by which distribution and serialization are embedded in Java show the advantage of a modern language designed to support these concepts.

JNI (Java Native Interface)

Often programmers can get frustrated when they wish to use the benefits of Java to do something that is system dependant. The JNI allows you to interface with a native-shared object and run its functions. This means you can write system-dependant code in C (or any other language) and use it from Java. Of course, as a result, your program would not be portable unless you supply the shared object to all platforms. This could be useful, for example, to catch signals in UNIX and to access the registry in Windows.

JDBC (Java Database Connectivity)

Java Database Connectivity is an SQL database access interface. It provides a database-independent way to perform SQL queries on any database that provides JDBC drivers. Currently, many popular databases do, and those that don't can still be accessed via the JDBC-ODBC bridge, which allows you to use ODBC drivers instead. For a list of database drivers see: http://java.sun.com/products/jdbc/jdbc.drivers.html.

Take a good look at Java. If we could manage to separate the applications from the operating systems running them, we'd have the freedom to choose shich OS we like best. Although in spirit the Linux community has a ``renegade'' non-conformist element in it, Java has a great potential and deserves our attention. The Linux-Java combination can turn into a winning one.

Java Resources

Java home: "http://java.sun.com/

Java developer connection (free registration): http://java.sun.com/jdc/

Swing (JFC): http://java.sun.com/products/jfc/index.html

Java for Linux: http://www.blackdown.org/java-linux.html

Copyright © 1998, Shay Rojansky
Published in Issue 32 of Linux Gazette, September 1998