Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

Steps to Java Internationalization (i18n)

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java Applets #4

Java Applets #3

Java Applets #2

Java Applets #1

Java #5 I Wanna hold your hand

You are here: irt.org | Articles | Java | Java #5 I Wanna hold your hand [ previous next ]

Published on: Friday 31st July 1998 By: Tarique Sani

Introduction

"I wanna hold your hand, Oh Yeah!" When I had started my series on Java and the Java FAQ, I was hoping that some one would rip my code apart and tell me how lousy programmer I am plus teach me a lesson or two. BUT nobody did. Which leads me to a conclusion that either my code was perfect (Very unlikely) or all the better Java programmers are busy earning their five figure salaries to bother about articles and FAQ's. However there was a demand for basic hand holding / getting started articles. So if you happen to be one of those who requested then ask your mom or dad to dig out that Beatles hit from the days of their school prom and get going.

Two Assumptions

I assume that you have already got a copy of JDK 1.1.6 (that’s the most popular current version) and want to get cracking. If you haven't then take a look at the resource section in Java FAQ to see where you can get it from. Frankly if you are a newbie web designer and havn't yet started on Java the I suggest you reconsider learning Java (Ask me why?) and instead utilise your time better in reading all the excellent articles on Java Script, XML, HTML Perl etc that we have got at irt.org for you, however if you feel like charging at the windmills then go ahead - May God help you.

I will further assume that you would be using either Windows 95 or Sun Solaris as your OS because though there are as many as 17 different Operating Systems which support Java, Sun Microsystem has released only three of them ( I am not including Mac here - Apologies to Mac Users).

Installing the JDK

JDK for Win95 comes either as a .zip file or .exe file. If it is an exe file simply run it in a DOS box from C:\ or D:\ or where ever you want it to be installed and that’s it! If it is a zip file unzip it in a directory of your choice. Use a newer version of WinZip (6.2 or 6.3 works fine) so that the long file names are preserved.

Sun's JDK for Solaris comes as a .tgz copy it to your home directory and use the following commands

gunzip ./jdk1.1.6.tgz

tar -xvf ./jdk1.1.6.tar

You will end up with a Java directory that contains the full JDK, if you are curious enough you will notice that the java/lib directory contains a classes.zip file - DO NOT UNZIP IT - it needs to in a zipped form for it to work correctly.

Configuring the JDK

With most distributions of JDK chances are that it has been correctly configured for you. Still it is recommended that you check the following:

Win95: JDK need two important modifications to your autoexec.bat file. Open your autoexec.bat file in notepad or wordpad and look for the PATH setting, it looks like:

PATH C:\windows; C:\DOS; C:\MyXratedgames; …

(Of Course you knew that already) somewhere in the line should be an entry for your java\bin directory, if it is missing simply add the full pathname of your JDK to the end of this line starting with a c: and ending with BIN. For example:

C:\myjavadir\BIN or C:\learing\JDK\BIN

The second thing that you will need to add to your autoexec.bat file (If it isn't there already) is a CLASSPATH variable. Search for a line that says:

SET CLASSPATH=C:\jdk\lib\classes.zip;.;

The CLASSPATH may also have other entries in it for Netscape or Internet Explorer. but we are most interested in the reference to the classes.zip file in the JDK directory and the current directory (.) If your autoexec.bat does not include either of these locations, add a line to the file that contains both (the above line will work just fine) After saving the autoexec.bat file, you will need to restart your computer for the changes to take effect.

Solaris: To configure JDK for Solaris all you need to do is add the java/bin or jdk/bin directory to your execution path. Usually a line like this in your .cshrc .login or .profile files will work:

set path = (~/java/bin/ $path)

assuming that your java files are indeed in the directory java.

Log out and log back in for the changes to take effect. Or use:

source ~/.login

Creating a Java Application

Ah! Finally we can get down to work. We will program the ubiquitous Hello World example.

remember we are going to write a Java Application rather than a Java Applet , but don't despair we will talk more about creating Applets in the part II of this article.

Creating the Source file

As with any programming language, the Java source files are created in plain text editor that means you are either working in notepad, wordpad OR emacs, vi or joe depending on your OS even DOS edit will do !

Now type in the following code exactly as below

class HelloWorld{
     public static void main(String args[]){
         System.out.println("Hello world!");
     }
}

Save the file somewhere on your disk with the name HelloWorld.java This is very important. Java source file must have the same name as the class they define (remember the case as well) and they must have the extension .java. If you name the file something else (even helloworld.java) you won't be able to able to compile it.

Compiling and running the source file

To compile your java program in Windows 95 start a DOS box , its under the Programs menu and is called MS-DOS Prompt (No! I don't consider you morons I just am finicky about some details).

Once inside DOS change directories to the location where you have saved your HelloWorld.java file I saved mine under MyJavaFiles on C: drive, so I use the command:

CD c:\MyJavaFiles

Once you are there, you can use the Java Compiler, Using the Java compiler is no big deal just use:

javac HelloWorld.java

Again making sure that you type all the upper and lower case properly here as well. If all goes well then you will end up with a class file, (nothing to do with the quality of file :-) called HelloWorld.class. this is your Java bytecode file. If you get any error, just repeat the above carefully again.

Once you have the class file ready, run it using the Java Bytecode Interpreter, another bombastic word! at the command prompt just type:

java HelloWorld

(Note: you don't have to type the extension class after the HelloWorld).

Congrats! if you see the phrase "Hello World" printed on your screen you have just successfully written and executed your first Java Application. The procedure for Linux is no different Use an Xterm window if you are using Xwindows to compile and execute the Java program other wise the Compiler and Interpreter run directly from the command prompt.

However if you were unable to compile or run this application - Wait for part II of the article in which we will write the Hello World applet and discuss about trouble shooting your first App!

Related items

Steps to Java Internationalization (i18n)

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java Applets #4

Java Applets #3

Java Applets #2

Java Applets #1

Feedback on 'Java #5 I Wanna hold your hand'

©2018 Martin Webb