Home Text Only Quick link to main content

Home | Services | Events | Features | Interviews | Profiles | Reviews | News | Resources | Press | Archive

Lecture 1 - Introduction to Java

Table of contents

1.0 Summary of lecture

2.0 Background

3.0 Books

4.0 Starting with Java

4.1 Installing the JDK

5.0 Your first Java applet

5.1 Compiling your program

5.2 Running your applet

6.0 Your first Java applet

6.1 Running your applet

7.0 Help file

8.0 Trouble shooting

8.1 Running the Java application

8.2 Can't find class

8.3 Running the Java applet

9.0 Summary


1.0 Summary of lecture

In this lecture we'll be taking a look at the background to Java. Seeing where it came from and its strengths and weaknesses. In the second half the installation of the development environment and creation of some test programs will be covered.

By the end of this lecture you will hopefully have a working Java compiler and virtual machine installed on your machine.

2.0 Background

In 1990 Sun Microsystems started developing a language that could be used with integrated systems to produce reliable products for the next generation household appliances, e.g. televisions, video recorders, washing machines etc.




This meant that the language would need to be architecture independent, i.e. the same program would run on many different chips without any changes being necessary. One day in 1993 the engineers were sitting in a cafe9 discussing the project. The Internet had just started take off and they struck upon the idea that a development language like the one they were writing would be ideal for writing applets that would be integrated with web pages. This language that resulted from this idea was named Java after the Java coffee bean, this also explains the logo.




One of the major plus points of the Java language is that it is platform independent. This means that software developers only have to write, distribute and maintain one copy of their software. The same program, without change, will run under most popular operating systems: Mac OS, MS Windows, variants of UNIX (Solaris, Linux etc.).



As web browsers on PDAs (Personal Digital Assistances) are getting more advanced, the same Java applets that runs within web browsers on desktop machines, will also work on your PDA.






For this reason alone Java as huge potential because due to the ever-increasing popularity in the Internet people are demanding this kind of portability from software.

To allow this the Java compiler generates bytecode, rather than native machine code. To actually run a Java program, you use a Java run-time system to execute the compiled byte code. A Java program can be run on any system that implements the Java interpreter and run-time system. Collectively, the interpreter and run-time system implement a virtual machine called the Java Virtual Machine (JVM). Along with portability Java's other strong points are:

  • Java is a fully object oriented language . This means the focus is on the data in the applications and the methods that manipulate that data. In an object-oriented system, a class is a collection of data and methods that operate on that data. Taken together, the data and methods describe the state and behaviour of an object. Classes are arranged in a hierarchy, so that a subclass can inherit properties and methods from its superclass.
  • Java was designed for writing highly reliable (robust) software. It does this by being a strongly typed language, which allows for extensive compiler-time checking for potential problems. Plus as Java does not support pointers, this eliminates the possibility of overwriting memory and corrupting data. The Java interpreter also performs a number of run-time check, such as verifying that all arrays and string accesses are within their bounds
  • Java has automatic garbage collection preventing memory leaks and other pernicious bugs related to dynamic-memory allocation and deallocation. This means that you do not have to worry about freeing memory up that you have used.
  • Java is a secure language. This is important as Java is designed for use in networked environments. Java implements several security mechanisms to protect you against code that might try to damage your computer system. As described above Java's memory allocation model is one of its main defences against malicious code.

    One down side is that a Java program executes about 20 times slower than a similar C one. This is because Java is an interpreted language unlike C, which is complied. However in situations where performance is critical, the Java designers are working on "just in time" compilers that can translate Java byte-codes into machine code for a particular CPU at run-time.

    3.0 Books

    There are a great number of books out there on the subject of Java, some are aimed at beginners and others at advanced users. In fact a search on for books on Java returned a list of over 700 books, although a lot of those where on Java Script or Java Beans* you can see that Java is a popular topic. Some of the books used in the creation of this course are listed here:

    Core Java 1.1 , Volume 1 : Fundamentals, Cay S. Horstmann, Gary Cornell

    Java, A Practical Guide , Neil Fawcett, Terry Ridge

    Java in a Nutshell , David Flanagan

    Developing Java Software , Russel Winder, Graham Roberts

    Java by Example , Jerry Jackson, Alan L. McLellan

    Developing Professional Java Applets , K. C. Hopson, Stephen E. Ingram

    * Note that some books cover the topic of Java beans (Java beans are essentially Java components) and Java script (Java script is code which is placed into actual code for the web page). These are not covered in this course and if your looking for a book to learn the Java programming language a book on these two topics may not prove that much use.

    Another great resource of information regarding Java is the Internet. As mentioned Java was created by Sun Microsystems, their main web site is www.sun.com . They also have a section dedicated to Java, the address to which is java.sun.com . If you can not find the answer to your question there are a large number of forums that you could try. The one run by Sun can be found at forum.java.sun.com .

    4.0 Starting with Java

    Before you can start to write Java programs you first have to install the Java Development Kit (JDK) on to your computer. For maximum compatibility this course and all the examples in it will confirm to version 1.0.2 of the JDK. Note: In this course when the acronym JDK is used it is referring to JDK version 1.0.2 unless explicitly stated.

    4.1 Installing the JDK

    The JDK is available for several platforms, including:

  • Microsoft windows 95/98/NT
  • Sun Solaris 2.3, 2.4, 2.5 SPARC
  • Sun Solaris 2.5 X86
  • Mac OS

    Since Microsoft Windows is the most common platform we'll now look at the steps involved in installing the JDK on Windows 95/98/NT.

    1) Download the JDK from Sun's website.

    2) Place the downloaded executable in your root directory of your C Drive (C:\).

    3) Run JDK-1_0_2-win32-x86.exe. This will extract the Java files to a directory called c:\java.

    4) Open the file c:\autoexec.bat in Notepad. Add the following lines to the end of the file:

    SET PATH=%PATH%;C:\java\bin
    SET CLASSPATH=C:\java\lib\Classes.zip

    5) Restart your computer to allow the changes made to take affect.

    The PATH variable tells Windows where to look for programs. Since we will need to access the Java compiler and virtual machine from other directories on your computer, Windows needs to know where to find it.

    The CLASSPATH variable tells the Java compiler where the standard class files that come with Java are located.

    5.0 Your first Java application

    Now that you hopefully have the JDK installed we need to make sure that it is working. To do this we'll run through the six steps required to create, compile and run a simple Java application. If you have any errors then have a read of the trouble shooting section at the end of this lecture.

    Step 1: Firstly you will need a place to store your applications so create a directory called JavaApps in c:\java. Next create a directory for your first application called FirstApp in the JavaApp directory (c:\java\JavaApps\FirstApp).

    The examples in this course are written using notepad but you can use any text editor as long as the files are saved as plain ASCII text.

    Start your text editor and enter the following text

     class FirstJavaApp  

    {
    1.
    public static void main (String args[ ])
    {
    1.
    System.out.println("Hello there!");
    }
    }
    Don't worry what this means as this it will be explained in a later lecture. The purpose of this exercise is to make sure your compiler and JVM work correctly.

    Step 2: Save the file as FirstJavaApp.java in the c: directory.

    Note : You should be careful to watch for Notepad placing an extra ".txt" at the end of the file name.

    5.1 Compiling your program

    Next you have to compile you program in the text file into Java byte code:

    Step 3: Bring up an MS DOS console. This is generally done by going to Start -> Programs -> MS-DOS Prompt.

    Step 4 : Go to the c: directory by typing the following at the console.

    C:
    cd

    Step 5: You now have to compile your program. So enter the following at the console.

    javac FirstJavaApp.java

    The program javac is the Java compiler that takes a text file as its input (as a parameter) and generates the corresponding byte-code. The byte-code file has the same name as the entered text file but with a .class extension.

    If everything has worked then after a few seconds the command line prompt should return and no errors should be shown. If an error is generated carefully check your code and the commands entered at the command prompt and try again. Remember that Java is case sensitive.

    If every thing has gone right there should be the file FirstJavaApp.class in the directory c:\Java\JavaApps\FirstApp. This is the byte code version of your program.

    5.2 Running your application

    Now you have this amazing byte-code file that can be run on any operating system, but how do you actually run it on your machine?

    Step 6 : Enter at the MS DOS command prompt:

    java FirstJavaApp

    You should see the words "Hello there" printed on the screen. As the following screen shot shows.




    Congratulations, you've now written, compiled and run your first Java application! As mentioned in the introduction Java works great over the Internet, the next section deals with writing your first Java applet. A Java applet is a program written in Java that will run in a web browser

    6.0 Your first Java applet

    You will need a place to put your applets so create a directory called JavaApplets in c:java. Also, create the directory FirstApplet in this new directory for the applet we are about to write (c:\java\JavaApplets\FirstApplet).

    Step 1: Start your text editor and enter the following text

     import java.awt.Graphics; 
    public class FirstJavaApplet extends java.applet.Applet
    {

    public void paint(Graphics g)

    {
    g.drawString("Hello there!", 20, 25);
    }

    }
    Step 2: Save the file as FirstJavaApplet.java in the

    c:\Java\JavaApplets\FirstApplet directory.

    Step 3 : Go to the c:\Java\JavaApplets\FirstApplet directory by typing the following at the MS-DOS console:

    C:
    cd:\Java\JavaApplets\FirstApplet

    Step 4 : Compile your applet by entering

    javac FirstJavaApplet.java

    Step 5: If everything has worked then after a few seconds the command line prompt should return and no errors should be shown. If an error is generated carefully check your code and the commands entered at the command prompt and try again. Remember that Java is case sensitive.

    Step 6: There should now be the file in the c:\Java\JavaApplets\FirstApplet directory called FirstJavaApplet.class. This is the byte code version of your applet.

    6.1 Running your applet

    Like before, you have this byte-code file. However, before you can run it in a web browser you have to create a web page that gives the browser information about your applet. To do this:

    Step 7: Start your text editor and enter the following text
    <HTML>
    <HEAD>
    <TITLE>My first Java applet</TITLE>
    </HEAD>
    <BODY>
    This is my first Java applet
    <BR>
    <APPLET CODE="FirstJavaApplet.class" WIDTH=150 HEIGHT=50>
    </APPLET>
    </BODY>
    </HTML>

    Step 7: Save the file as FirstJavaApplet.htm in the c: directory.

    Step 8: Load this file up into your favourite web browser and you should see the applet running.





    Click here
    to go to a web page running this applet.

    7.0 Help file

    The help file containing detailed information about the different classes can be downloaded here . Unzip the file to a directory, for example

    c:\jdk-1_0_2-apidocs\

    The help file is not that much use in learning to program Java but when we get to looking at user interface controls it will become more useful as a reference guide .

    8.0 Trouble shooting

    Hopefully you have not had any problems installing the JDK or creating, compiling and running the examples. However, if you had take a look to see if any of your problems are here .

    8.1 Running the Java application

    8.1.1 Class not found error


    You getting are the error:

    Exception in thread "main" java.lang.NoClassDefFoundError: FirstJavaApp




    This error is generated when the JVM cannot find your program. To solve this you will have to add the directory

    C:\Java\JavaApps\FirstApp

    to the class path in your autoexec.bat file. This means altering the line

    SET CLASSPATH=C:\java\lib\Classes.zip

    to

    SET CLASSPATH=C:\java\lib\Classes.zip; C:\jJava\JavaApps\FirstApp

    Restart your machine and try again .

    8.2 Can't find class

    You getting are the error:

    Can't find class FirstJavaApp




    This error is generated when the JVM cannot find your program. To solve this you will have to add the directory C: to the class path in your autoexec.bat file. This means altering the line

    SET CLASSPATH=C:\java\lib\Classes.zip

    to

    SET CLASSPATH=C:\java\lib\Classes.zip; C:\Java\JavaApps\FirstApp

    Restart your machine and try again .

    8.3 Running the Java applet

    8.3.1 Web browser reports an error

    Because Java has a high level of security built in some web browsers won't always run Java applets because they think that they might be doing something they shouldn't. To get around this you can either lower the security level of your web browser or run the applet using the applet viewer provided with the JDK. To use the applet viewer enter AppletViewer followed by the web page at the command prompt. As shown in following example. A new window will now appear with your Java applet running inside it .




    9.0 Summary

    In this lesson you were shown how to start programming in Java. This involved downloading the JDK, installing it and setting in up. You wrote, compiled and ran your first Java application and Java applet, which ran in a web browser.

    If you have got this far and the examples are working on your machine then you are well on the way to writing Java programs. Since this course is Internet based we will be concentrating on Java applets and in the future lectures we'll cover topics such as creating a user interface, reading and writing to files and networking .



  • Home | Services | Events | Features | Interviews | Profiles | Reviews | News | Resources | Press | Archive
    About ITWales | Privacy Policy

    All material on this website ©2002-2008 ITWales
    spacer

    Search ITWales

    Advanced Search
    envelope Subscribe to
    ITWales Updates
    Click Here!