Running Java Programs under Mac OS 9.x

Updated November 15, 2001

If you have comments or suggestions for improvements to this page, please send them to [email protected]



Limitations to Using Java

There are some limitations that you will encounter when programming in Java:



Introduction:

This page documents all the steps required to compile and execute Java programs, including Swing programs, under Mac OS 9.x.   It also covers the construction of Java applets and how to run them successfully using either Internet Explorer or Netscape Navigator.  Finally, it provides detailed instructions for building applications using JDBC compliant databases which are running either locally or remotely.

Notes:



Software Required:

You first need to download some software.  The runtime environment and Java software development kit can be found at

http://developer.apple.com/java/classic.html

The current versions are;

MRJ 2.2.5 - runtime environment, usually referred to as a Java Runtime Environment (JRE)

MRJ SDK 2.2 - Software Development Kit needed to compile and run Java applications

By default the runtime environment will be installed into the folder MRJ Libraries in the System -> Extensions Folder.  The Software Development Kit components will be installed into the folder MRJ SDK 2.2 on your main disk drive.

Note that the SDK 2.2 does NOT include Swing classes.  In order to get those you have to find the Java Foundation Classes page on the Sun site.  Currently it is located at

http://java.sun.com/products/jfc/download.archive.html#1.1.1

Towards the bottom of this page you choose MacOS for the Platform and press the continue button.  That will take you to the actual download page;

http://www.sun.com/Download3

By default the swing components will be installed into the folder Swing-1.1.1 in the Applications (Mac OS 9) folder.

In order to test that the Java SDK and runtime environments have been installed correctly, type the following program into SimpleText and save it with the file name HelloWorldApp.java into some development folder that you create to do your work (note that it is essential that you use a file name that is exactly the same as your main Java class - in this case HelloWorldApp).

/**
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
//  Display the string.
    }
}
}

Drag the file HelloWorldApp.java onto the application javac in the JDK Tools folder.  Clicking on the Do Javac button will cause the file to be compiled.  The following messages will be printed to the Java Console window, and the file HelloWorldApp.class will be written to your development directory.
 

Executing:  Javac -classpath /Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/JDKClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/JDKToolsClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses Folder/:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses Folder.1/:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/QTJava.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/ /Macintosh HD/User Data/Davis's stuff/Java/HelloWorldApp.java
Done
 


 
 
 
 
 
 

Note: On my brand new iMac Indigo the javac application completely freezes the machine once in a while.  I found that it is best to not generate mouse events while the compilation is taking place.

Unlike a Windows or Unix machine, you now have to wrap the Java class in an application to run it under Mac OS.  This can be done by dragging the file HelloWorldApp.class onto the application JBindery in the Application Builders folder of the JDK.  Clicking on the Run button will cause the following message to be written to the Java Console Window.

Hello World!

Congratulations!  You have now run your first Java program under Mac OS 9.x.



Running Swing programs;

First you need a demonstration program.  I used the program GridBagWindow.java which can be found on the page

http://java.sun.com/docs/books/tutorial/uiswing/layout/example-swing/index.html#GridBagWindow

Download this file to your development folder.   If you now drag and drop the file GridBagWindow.java onto the application javac in the JDK Tools folder, you will get the following messages written to the Java Console Window.

Executing:  Javac -classpath /Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/JDKClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/JDKToolsClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses Folder/:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses Folder.1/:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/QTJava.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/:/Macintosh HD/User Data/Davis's stuff/Java /Macintosh HD/User Data/Davis's stuff/Java/GridBagWindow.java
/Macintosh HD/User Data/Davis's stuff/Java/GridBagWindow.java:7: Package javax.swing not found in import.
import javax.swing.*;
       ^
/Macintosh HD/User Data/Davis's stuff/Java/GridBagWindow.java:9: Superclass JFrame of class GridBagWindow not found.
public class GridBagWindow extends JFrame {
                                   ^
2 errors
Done

These messages are basically telling you that the compiler cannot find the Swing classes.    Try dragging the file again, but this time use the Add button on the javac application to add the class file swingall.jar ( by default this is in Applications (Mac OS 9) -> Swing-1.1.1).  You should see the complete path in the Classpath window at the bottom of the javac application.

Clicking on the Do javac button should now produce the following result.

Executing:  Javac -classpath /Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/JDKClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/JDKToolsClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses Folder/:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses Folder.1/:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/MRJSDKClasses.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/QTJava.zip:/Macintosh HD/System Folder/Extensions/MRJ Libraries/MRJClasses/:/Macintosh HD/User Data/Davis's stuff/Java:/Macintosh HD/Applications (Mac OS 9)/Swing-1.1.1/swingall.jar /Macintosh HD/User Data/Davis's stuff/Java/GridBagWindow.java
Done

Note:  You will find that this compilation creates two files, GridBagWindow.class and GridBagWindow$1.class.  I don't know why.

If you now drag the file GridBagWindow.class onto the application JBindery in the Application Builders folder of the JDK and click on the Run button, you will get a pop-up window with he following message.

Unable to launch Java application

The main class "GridBagWindow" could not be loaded.
Either it is not in the classpath or it has a static initializer
that uses a class not in the classpath.

This message is just indicating that the Swing components are not in your classpath.  Try dragging the file again, but this time click on the Classpath icon on the left side of the JBindery application and use the Add Folder button to add the file swingall.jar to the Classpath.  This time the application should come up properly when you click on the Run button.

Congratulations !  You have just run a Java Swing application under Mac OS 9.x.

You can save your application, complete with correct classpath, by using the Save Settings button on the JBindery application.  Then you can just double click on your application file to launch it.



Running Applets:

Now let's try running some Java code in a Web browser, rather than by using JBindery.  There are a number of examples located at;

http://java.sun.com/applets/jdk/1.1/

Download NervousText.java, save it in your development directory, then compile it using javac as described above.

Open SimpleText and type in the following and save it in a file called NervousText.html in the same directory as you have NervousText.class.;

<title>Nervous Text 1.1</title>
<hr>
<applet code="NervousText.class" width=375 height=50 alt="If you see this message, your Browser can't run the applet">
<param name=text value="I really love Java">
If you see this message, your Browser doesn't understand APPLET tags.
</applet>

Using your Browser, open the file NervousText.html and you should see the dancing text.  This works with Internet Explorer 5 that is pre-installed on newer Macs.

Java applets do not run under Netscape 4.7 which is installed with newer Macs.  There are two different problems.  One is that the applets are invoked with the obsolete <EMBED> tags.  The second is that there is probably no Java Virtual Machine installed.   The best solution to these problems is upgrade to Netscape 4.78.  The download for this product is at

http://home.netscape.com/download/index.html

The upgrade is quite straightforward.  You probably want to move your current Netscape Communicator Folder to a backup area before doing the upgrade.  Once you have upgraded Netscape, you need to download the MRJ Plugin, which will provide the Java Virtual Machine for your Browser.  This can be found at

http://www.mozilla.org/oji/MRJPlugin.html

The download will create a new folder called MRJPlugin on your desktop.  Be sure to copy both the MRJPlugin (4.X) and MRJPlugin.jar files to the Plug-ins folder inside the Netscape Communicator Folder.

Start up Netscape Navigator.  Check the Help -> About Plug-ins to make sure that you have access to the MRJ Java Plugin.  Then go to Edit -> Preferences and click on the Advancedd label to bring up the dialog labeled

Advanced - Change preferences that affect the entire product.

Check off the box labeled Use Java Plug-in.

Now you should be able to open up the file NervousText.html in Netscape Navigator and see the dancing text.

Congratulations!  You have now viewed a Java applet in both Internet Explorer and Netscape Navigator under Mac OS 9.x.



Publishing Your Java Applications to the Internet

Copy the files NervousText.html and NervousText.class to the Web Sharing folder (typically in the Documents folder on your main disk drive).  Now open up the Web Sharing control panel, Select your Home Page to be NervousText.html and press the Start button.   You should now be able to open up your Java applet from within either IE or Netscape using HTTP protocol by entering http://111.222.333.444/NervousText.html into the location field (Note:  the IP address 111.222.333.444 needs to be replaced with your IP address, which will be displayed in the Web Sharing control panel).   Even more impressive, this will work from any other Java enabled browser on the Internet!

Important Note:  You should make sure that you have network security software or hardware in place before you begin publishing Web pages to the Internet.  I use software from Sustworks.com, which was reasonably priced, allows me to share my Internet connections, and prevents unauthorized access to my local area network.



Connecting to an Oracle Database with JDBC

First, you will need to download the JDBC "thin" drivers from the Oracle Technet site.  Registration at the site is free but downloads are to be used for non-commercial, evaluation purposes only.

Finding the driver is a bit tricky since Oracle does not refer to Mac OS explicitly anywhere.  However, the page

http://otn.oracle.com/software/tech/java/sqlj_jdbc/content.html

refers to "Version 8.0.5.2 JDBC(Thin and OCI) for Solaris" which is what you want.  Clicking on this link and agreeing that you meet the "ELIGIBILITY EXPORT RESTRICTIONS" will take you to a page listing numerous versions of the Oracle JDBC drivers.  I chose to download the "JDBC-Thin, 100% Java ( 1.7MB)" driver.

Note:  I was not able to use Netscape to download this file.  However, by using IE 5, holding the mouse button down over the link, and choosing "Save this Link to Disk" I was able to get the file classes111.zip onto my desktop.  You will want to copy this file into the development area where you are storing your own class files.

You will need to have a remote Oracle database up and running, complete with a Listener process, before you can build your test application.  I installed Personal Oracle 8 on a Windows '95 machine.  Apart from having to configure the Listener and enabling two-phase commits, the installation was very straightforward.

The following example code will create a connection to a remote Oracle database and display the results of a query;

/*
 * This example Applet connects to an Oracle database via the Oracle thin
 * JDBC drivers and prints a value selected from the database.
 * Assuming that the Oracle database is running on a remote sever, this applet will only
 * work if you use File -> Open -> Page in Navigator in Netscape, or File -> Open File in
 * Internet Explorer.  This applet can NOT be served up as an HTTP page because of security
 * constraints on served applets.
 */
import java.sql.*;
import java.text.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.Graphics;

public class JDBCTestApplet extends Applet {
   TextArea msgArea = new TextArea(20,20);
//
// The methods init, start, stop, and destroy are all methods of the applet class
//
   public void init() {
//
//     Add the text area to the default container
//
       setLayout(new GridLayout(1,1));
       add(msgArea);
   }

   public void start() {
       outputTextLine("Starting the Applet ...");
//
//     Load the Oracle JDBC driver
//
       try
         {   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//
//          Set connection information
//          REPLACE THE USER, PASSWORD, IP ADDRESS, AND INSTANCE WITH ONES
//          APPROPRIATE FOR YOUR ORACLE ENVIRONMENT
//
            String user = new String("Scott");
            String password = new String("Tiger");
            String database = new String ("192.168.111.222:1521:ORCL" );
            String msg = new String("Connecting to " + database + "\n"
             + "The following line(s) will be the result of the query ... please wait\n"
             + "------------------------------------------------------------------------");
            outputTextLine(msg);
            Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@"
                                       + database, user, password);
            Statement stmt = conn.createStatement ();
//
//          Get a set of rows from the database
//          REPLACE THIS QUERY WITH ONE APPROPRIATE FOR YOUR DATABASE
//
            ResultSet rset = stmt.executeQuery ("select ENAME from EMP ");
            while (rset.next ())
            {
               outputTextLine(rset.getString (1));
            }
            conn.close();
          }  catch (SQLException ex ) {
             outputTextLine(ex.getMessage());
          }
   }
   public void outputTextLine(String outputString) {
        msgArea.appendText(outputString + "\n");
        repaint();
   }
}

Compile this program using javac, remembering to add the file classes111.zip to your CLASSPATH.  You will probably get a warning message which can be ignored.   Open SimpleText (or Notepad in Windows) and type in the following;

<title>JDBC Test Applet</title>
You should a report below
<hr>
<applet code="JDBCTestApplet.class" ARCHIVE="classes111.zip" width=375 height=50
alt="If you see this message, your Browser can't run the applet">
<param name=text value="I really love Java">
If you see this message, your Browser doesn't understand the APPLET tag.
</applet>

Save this file with the name JDBCTestApplet.html in the same folder as the files JDBCTestApplet.class and classes111.zip.  You should now be able to use File -> Open from either Netscape or IE to run this applet and see the results of your query.

Congratulations !  You have now built and run a JDBC application accessing a remote Oracle database.



Web Enabling JDBCTestApplet

Because Oracle doesn't run under Mac OS (at least I wasn't able to find a Mac release), and because it is necessary for both the Web Server and DBMS to be running on the same machine in order to allow JDBC access from applets, I had to reconstruct JDBCTestApplet on my Windows '95 machine.  Luckily, this turned out to be fairly trivial.  I downloaded the classes111.zip file for Windows from the Oracle technet site, as described above.  The current location for this file is

http://otn.oracle.com/software/tech/java/sqlj_jdbc/htdocs/winsoft.html

The JDBCTestApplet.java file was then compiled using with the following compiler command:

javac JDBCTestApplet.java -classpath classes111.zip

By placing the files JDBCTestApplet.class, JDBCTestApplet.html and classes111.zip into the default Webshare directory on my Windows '95 machine (that was also running the Oracle DBMS) I could run this JDBC applet from any other machine on my network.

Note:  The Personal Web Server that comes with Windows '95 will not serve up Java applets.  I had to download the Apache Web Server from www.apache.org.  This installation was extremely easy (remember to upgrade your socket software first, as indicated in the download instructions) and the Web sever works very well.

Congratulations!  You have now deployed a Web-enabled Java applet that connects to Oracle using JDBC.


If you have comments or suggestions for improvements to this page, please send them to [email protected]

1