Introduction to Java

From EDM2
Jump to: navigation, search

Written by: Carsten Whimster

Introduction

With all the hype surrounding Java these days, it is hard to tell truth from fiction and hyperbole. I decided to cut through it all myself, and see what all the fuss was about. Even though the JDK (Java Developer Kit) has been out for a while for Win95 and WinNT, they don't seem to have gotten very far with it, judging by the quality of the questions on the Java newsgroup (comp.lang.java), and we OS/2 programmers are in relatively good shape for catching some early action. Recently the 1.0 release has been finished by Sun, but OS/2 is not included in this release yet, so we still have to contend with the beta release. From the sounds of the fury on the newsgroup, this is not a big deal, as there are plenty of problems with the gold release anyway.

This document is meant to guide the Java novice through getting the necessary files, installing Java on the computer, and checking that everything works as planned, including writing a quick program and a quick applet.

Getting the Ingredients

To get the files you need, it is necessary to register with IBM first. This is due to the licence agreement with Sun, which dictates these terms. It doesn't cost anything, and IBM won't hold it against you or scrutinize your intentions; basically it is open to anyone. Here is how to proceed:

First point your favorite browser at mailto:majordomo@hursley.ibm.com with the text: subscribe list-name or: unsubscribe list-name in the body of the mail. You may include multiple requests in a single piece of email. More information on the mailing lists can be found at http://ncc.hursley.ibm.com/javainfo/forums.html.

After going through the signup procedure, you will eventually receive e-mail telling you that you now have access to the JDK Developer pages on the IBM Java server at [1].

Here you will find a link to both the OS/2 and AIX areas. (I presume you will now choose OS/2). Then you get to choose between reading the IBM "readme" file, the Sun "readme" file, downloading the beta (3.8Mb), and downloading the API documentation in either HTML (374kb) or Postscript format (1.2Mb). Get the beta, and one of the documentation packages.

This will leave you with two files on your harddrive:

1b1-os2.zip (3836026 bytes)
jdk-beta-apidocs.zip (374962 bytes)

(I presume, since you obviously have HTML capability)

I also went to Sun (http://java.sun.com/) and got the tutorial (881239 bytes, ftp://ftp.javasoft.com/docs/tutorial.html.tar.Z or ps.Z if you want Postscript and the beta 2 documents (685299 bytes, http://java.sun.com/JDK-beta2/index.html), to supplement what I got from IBM. Note that these documents are independent of IBM's OS/2 port, and there may be differences. They are quite useful, though.

There was also an alpha port circulated on Hobbes and FidoNet called os2java.zip (4889461 bytes), but as this represents an earlier, out-of-date version of Java for OS/2, its only value is in the fact that it contains HotJava, the Java enabled web- browser. This version of HotJava is incompatible with the beta toolkit, however, and you will only be able to use it to view earlier applets, not the ones developed with this beta, and there probably aren't many of those around any more. In addition to this, the release was unauthorized and should not be redistributed. If anyone asks me, I don't have a copy. Netscape's latest browsers supports the beta applets, by the way, but it is buggy, and incomplete in its support.

Now you have all the files you need.

Setting Up the Coffee Maker

Now that you have all the files, where should you put stuff? The first step is to install the 1b1-os2.zip file. When you unzip this (if you still use pkunzip, use the -d option, and switch to InfoZip as soon as possible), it creates a javaos2 directory along with a bunch of subdirectories. I would suggest putting the zipfile in either a root directory, or in a subdirectory you have for developing. After you have unzipped this file, you will find a file called javaos2/readme.ibm which has further instructions. This is the same as the IBM readme file on the web site, by the way. If you continue on with this article, you can forget about this file for now, but you should probably read it and the Sun file as well eventually.

Now you need to add the proper directories to your config.sys, i.e. add the DLL directory to the LIBPATH (eg, E:\javaos2\dll) and add the BIN directory to the PATH (eg, E:\javaos2\bin). The latter is not stricly necessary, but will make your life easier, and I will assume that you have done this for the rest of this article.

Before you reboot, you can also unzip the JDK documentation. This should be done in the javaos2 directory, since it creates a directory called apidocs and puts everything within this.

There is a file called javaos2/index.html but I found this of such limited use that I completely butchered it, and added all the useful links to it myself, including links to the API documentation. There is a file called javaos2/apidocs/API_users_guide.html, but this too is of limited value. I performed a few contortions to get the API documentation into the index.html file, and I have included here a quick-n-dirty description of how to do this. First, go to the apidocs directory, and type (I hope you have the GNU ls utility) 'ls -1 >apis'. This puts all the relevant filenames in a file. Edit this file and remove all references to non-html files, which should be really easy with grep or even manually. Then run the following REXX script in the same directory like this 'fix >api.out':

 /* fix.cmd */
 
 filename='apis'
 Call charin filename, 1, 0
 do 236
    thisline  = linein(filename)
    say '<li><a href="' || 'apidocs/' || thisline || '">' || ,
       substr(thisline, 1, (length(thisline)-5)) || '</a>'
 end
 
 charin( sceneFile, , chars( sceneFile ) )
 
 Call STREAM filename, 'C', 'CLOSE'

Figure 1: Creating a useful version of the Java reference.

Note the hardcoded value 236. If there are more files in your directory for whatever reasons, adjust this number to suit (I told you it was quick-n-dirty). This creates a file called api.out which contains lines like this:

[apidocs/API_users_guide.html API_users_guide]

and this is perfect for cutting and pasting to the index.html file. Make it so. Then you reboot, and now you are pretty much ready to try it all out.

Tasting the Brew

Here is a picture of what my Java folder looks like:

IntroJava-java1.gif

Figure 2: My Java folder.

As you can see, I have created a few convenience program icons. The YAOS icon gets me a command prompt in the Java directory, which is handy because Java is a commandline compiler. A free cup of coffee to the first person who can set up a Javac icon which compiles the file that is dropped on it (javac is the commandline compiler, a-la cc for C) in a convenient manner.

Another icon I have is the WebEx icon which brings up the local index.html file. I have included my index.html file with this article [ java.zip - Editor]. I guarantee nothing about the suitability of this index file to your setup, but if you have followed along, you can probably just drop it in as is. The picture of Java was obviously scrounged from the distribution itself, but there is a copy of the converted BMP (also in the zip). Finally, I made a quick-n-dirty icon which is also available.

Now that everything is gussied up, you can test the functionality.

(For the examples below, I have created a directory called Projects, as you can see in the folder.)

For my first trick [grin], I will write a short Java program, not an applet.

I should probably quickly explain that there are two kinds of programs you can create with Java: 'bytecode programs', which are the equivalent to pre-digested GWBasic programs (ie. more or less stand-alone applications, but in need of an environment to run in), and 'bytecode applets'. Applets is where all the fuss is, as they can be incorporated into web pages as runnable programs in small windows within the web page you embed them in. These will be sent by the web server to you, just like a graphic, when you access the page they are resident on. Then they will run on your browser, provided it is Java-enabled. The only Java-enabled browser that I am aware of is Netscape. HotJava is not yet done for the beta or release-level code, and that is not just for OS/2. I believe that Sun decided that since Netscape provided a browser with this code, the pressure was off them to complete HotJava, and they moved on to more pressing issues. [Last minute update: Sun has apparently completely revamped HotJava, and now aims it more as a competitor to Netscape with lots of neat functionality - Carsten].

The concepts behind programming programs and applets don't differ much, but I will start with the simpler of the two, the "stand-alone" program.

Type this code into a file called MyFirst.java:

 class MyFirst {
      public static void main(String args[]) {
     MyClass instance1 = new MyClass();
     MyClass instance2 = new MyClass("First class.");
     MyClass instance3 = new MyClass("Second class.", 2);
 
     System.out.println(instance1.GetString());
     System.out.println(instance1.GetInt());
     System.out.println(instance2.GetString());
     System.out.println(instance2.GetInt());
     System.out.println(instance3.GetString());
     System.out.println(instance3.GetInt());
      }
 }
 
 class MyClass {
      protected String string = "My first Java application";
      protected int integer = 0;
 
      public MyClass() {
      }
 
      public MyClass(String initString) {
     string  = initString;
     integer = 1;
      }
 
      public MyClass(String initString, int initInt) {
     string  = initString;
     integer = initInt;
      }
 
      public String GetString() {
     return string;
      }
 
      public int GetInt() {
     return integer;
      }
 }
 
 class MySubClass extends MyClass {
      private float floater;
 
      public MySubClass(String initString, int initInt, float initFloat) {
     string  = initString;
     integer = initInt;
     floater = initFloat;
      }
 }

Figure 3: My first Java applet.

Compile it as follows:

javac MyFirst.java

Figure 4: Compiling the code in figure 2.

Note the .java on the end of the filename. This is important.

This doesn't produce any output, other than the following files:

  • MyClass.class
  • MyFirst.java
  • MySubClass.class
  • MyFirst.class
  • MyString.class

Now run the application as follows:

java MyFirst

Figure 5: Running the applet.

Note the inconsistent absence of a .class extension. This is also important.

This should produce the output:

      My first Java application
      0
      First class.
      1
      Second class.
      2

Figure 6: The output produced.

That is all for my first example. Java is a fully object-oriented language, so if you never bothered to learn classes before, now is the time. Since this is not a Java language tutorial, I will not explain the above example, but if you know any C++ at all, you can easily see what it does, and some of the neat features of Java are quite apparent. Try changing various things in the program, and just mess with it in general. Java is surprisingly intuitive, and I was able to do most of the above without actually reading much.

Checking Out Other Flavours

Now we should do an applet. Since there is no HotJava in this release, Sun had to do something else to let us test our Java applets, and so they included a program called applet.exe which does just that. Type in the following code:

 import java.awt.Graphics;
 
 public class HelloEdm2 extends java.applet.Applet {
    public void init() {
       resize(150, 25);
    }
    public void paint(Graphics g) {
       g.drawString("Hello EDM/2!", 50, 25);
    }
 }

Figure 7: A more complicated example.

Compile it as usual:

javac HelloEdm2.java

Figure 8: Compiling the example.

This yields a class file. Now we need an HTML file which uses this applet:


<HTML>
<HEAD>
<TITLE>Your first Java applet</TITLE>
</HEAD>
<BODY>
Here is the EDM/2 applet:
<APPLET CODE="HelloEdm2.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>

Figure 9: An HTML file to test the applet.

Now you can test the applet by running this:

applet Edm2.html

Figure 10: Testing the applet.

Notice that you now include the .html extension, and that the HTML file does not have to be named by the class any more. This is necessary if more than one applet is to co-exist on one web page. This yields a window like this (after getting rid of a licence window).

IntroJava-java2.gif

Figure 11: The output of our second example.

Before I wrap up, I should mention that there is a third type of app for us OS/2 users, namely the PM application. When you use any PM functionality, you need to run it using the javapm command, rather than the java command. This is exclusively an OS/2 issue, and will be resolved before the golden release. This is beyond the point at which I am, though, so I will not be able to go into at this time.

Summary

Well, there you have it. Java as presented by the makers of some of the finest beans in the world. If you had any problems at all, check over everything you did carefully. Note that Java needs HPFS, and that it is case-sensitive. If you can't find your problem, and you want to mail me, go ahead. I probably will not be able to help you, though. A better bet is to get on the OS/2 Java mailing list mentioned above, and ask there. Check over the IBM and Sun sites frequently for more info on Java, and for programs you can try out. You can try reading comp.lang.java, but it is almost as noisy as the alt.games.doom hierarchy, which is to say 'very'. About 150 new messages appear here every day, most pure cruft.

There are a few limitations to this release, all of which are documented in the files that come with the beta JDK. One major limitation is that awt.image is not done yet [Last minute update: IBM has now made this available where the JDK is available]. Another is that JDB, the debugger, is unrealiable. Finally, only one instance of Java may be run at one time. You can find out all about these and the other limitations in the documentation. Happy sipping!