1
28

One step in Java

Becoming a Java programmer starts by installing the developer's work environment. Write and compile a first independent program, then a first applet which can be run in a navigator and you will be put once and for all on the right track.

Work environment

Download the latest Java Developer's Kit (JDK Standard Edition).

For Linux, install it in /usr:

$ cd /usr
$ chmod 755 /downloads/jdk-*-linux-i586.bin
$ sudo /downloads/jdk-*-linux-i586.bin
$ ln -s jdk-* java

The symbolic link will allow to always refer to the directory /usr/java, whatever the version of the JDK is installed.

For Windows, create a folder named C:\Program Files\Java, run the installer jdk-*-windows-i586.exe and instruct it to copy the package in this folder.

Download the documentation jdk-*-docs.zip and install it in /usr/java/docs under Linux, C:\Program Files\Java\docs under Windows. Search the page "The Java Tutorials" on the web site. Study them regularly, especially the introduction to Swing. You can also download the tutorials and copy them in /usr/java/tutorials under Linux, C:\Program Files\Java\tutorials under Windows.

Create a directory called Java in your home directory to store all your work.

Under Linux, make sure that the standard editor gedit is installed. Under Windows, install Notepad++ or TextPad. NOTE: You can also use Eclipse after you have installed the version for Java SE developers.

Under Linux, add the following lines to the file ~/.bashrc:

# set PATH and JAVA_HOME for Java
if [ -d /usr/java/bin ] ; then
    PATH=/usr/java/bin:"${PATH}"
    export JAVA_HOME=/usr/java
fi

Start a new terminal and check that commands from the JDK are accessible:

$ java -version
java version "1.6.*"
Java(TM) SE Runtime Environment (build 1.6.*)

Under Windows, create a file called env.bat in your Java folder with the following content:

  1. @echo off
  2.  
  3. set PATH=%SystemRoot%\system32;%SystemRoot%
  4.  
  5. echo Setting environment for Java (PATH JAVA_HOME).
  6. set JAVA_HOME=%ProgramFiles%\Java\jdk
  7. set PATH=%JAVA_HOME%\bin;%PATH%
  8.  
  9. echo Setting environment for Ant (PATH ANT_HOME).
  10. set ANT_HOME=%ProgramFiles%\Java\ant
  11. set PATH=%ANT_HOME%\bin;%PATH%

This file groups a series of commands which initialize the work environment.

Create a shortcut in the same folder. Configure the target so that it executes the order %comspec% /k env.bat. This line launches the command processor and tells it to start by running the content of the file env.bat. Rename the shortcut to Command Prompt.

To launch the command processor with the environment for Java, double-click on Command Prompt:

Setting environment for Java (PATH JAVA_HOME).
Setting environment for Ant (PATH ANT_HOME).
C:\Documents and Settings\frasq\My documents\Java>

Check that the commands from the JDK are accessible:

C:\Documents and Settings\frasq\My documents\Java>java -version
java version "1.6.*"
Java(TM) SE Runtime Environment (build 1.6.*)
Java HotSpot(TM) Client VM (build 16.*, mixed mode, sharing)
First program

Start with the "Hello" program, a classic which will let you check that the work environment is operational. Create a folder Hello in your folder Java.

$ cd
$ cd Java
$ mkdir Hello

Edit the file HelloWorldApp.java with the following content:

  1. public class HelloWorldApp {
  2.     public static void main(String[] args) {
  3.         System.out.println("Hello World!");
  4.     }
  5. }

NOTE: Don't type in the line numbers.

To run this program, first compile it:

$ javac HelloWorldApp.java

The Java compiler javac generates the file HelloWorldApp.class. This file contains the code for the JVM (Java Virtual Machine), the program which interprets the compiled code and runs it.

Run the compiled program with the following command:

$ java HelloWorldApp
Hello World!

Notice that the extension .class of the file isn't specified. NOTE: Passing HelloWorldApp.class to the interpreter would ask it to run the main method of the internal class named class defined in the class HelloWorldApp.

First applet

An applet is a program which is run by a plugin in a navigator. Create a file called HelloWorld.java:

  1. import java.applet.Applet;
  2. import java.awt.Graphics;
  3.  
  4. public class HelloWorld extends Applet {
  5.     public void paint(Graphics g) {
  6.         g.drawString("Hello world!", 50, 25);
  7.     }
  8. }

This file defines a class HelloWorld which inherits from the class Applet. The method paint is automatically called when the program needs to draw the content of the window. Notice that an applet doesn't have a main method.

Compile the program:

$ javac HelloWorld.java

To run the applet in a navigator, create a file called HelloWorld.html:

  1. <html>
  2. <head>
  3. <title>Hello Applet</title>
  4. </head>
  5. <body>
  6. <applet code="HelloWorld.class" width="150" height="25"></applet>
  7. </body>
  8. </html>

The tag <applet> asks the navigator to run the program at the address given by the attribute code. Note that the location of the code is relative to the location of the document.

Open HelloWorld.html from a navigator or pass it the name of the file in argument:

$ firefox HelloWorld.html

Another possibility is to use the program appletviewer provided with the JDK:

$ appletviewer HelloWorld.html

Add the following line at the very beginning of the file HelloWorld.java:

  1. // <applet code="HelloWorld.class" width="150" height="50"></applet>

This line, which is commented out for the Java compiler, is identical to the one which describes the applet in HelloWorld.html.

To run the applet, enter the following command:

$ appletviewer HelloWorld.java

The first line of the source file is read by appletviewer which runs it like a navigator would. No need to edit an HTML file to do a simple test.

First delivery

To package all the files of an application in a single file, first create a manifest file called HelloWorldApp.mf:

  1. Manifest-Version: 1.1
  2. Main-Class: HelloWorldApp

The clause Main-Class gives the name of the class which defines the main method, the entry point of the program.

Create a .jar with the manifest and all the files of the application:

$ javac HelloWorldApp.java
$ jar -cmf HelloWorldApp.mf HelloWorldApp.jar HelloWorldApp.class

To list the content of the .jar, run the following command:

$ jar -tf HelloWorldApp.jar
META-INF/
META-INF/MANIFEST.MF
HelloWorldApp.class

The archive, in fact a directory in a file, can contain .class files and any other type of files like configuration parameters, messages, images or sounds.

To run the .jar, pass the file with the -jar option to the interpreter:

$ java -jar HelloWorldApp.jar

You can pack an applet and all its files in a .jar too. The download will take a little more time but all the necessary files will be there when the program starts interacting with the user. Create the manifest:

  1. Manifest-Version: 1.1
  2. Main-Class: HelloWorld

Put all the files in a .jar:

$ javac HelloWorld.java
$ jar -cmf HelloWorld.mf HelloWorldjar.jar HelloWorld.class

To run the applet in a navigator, create a file called HelloWorldJar.html:

  1. <html>
  2. <head>
  3. <title>Hello Archive</title>
  4. </head>
  5. <body>
  6. <applet archive="HelloWorldApp.jar" width="150" height="25"></applet>
  7. </body>
  8. </html>

The name of the .jar is specified by the archive attribute.

$ firefox HelloWorldJar.html
Using Ant

Try Ant to rebuild an application automatically.

Download Ant.

Under Linux, install the tool in /usr:

$ cd /usr
$ sudo tar -jxvf /downloads/apache-ant-*-bin.tar.bz2
$ mv apache-ant-* ant-*
$ ln -s ant-* ant

Under Windows, install the tool in C:\Program Files\Java. Rename the folder apache-* to ant.

Under Linux, add the following lines to the file ~/.bashrc :

# set PATH for Apache Ant
if [ -d /usr/ant/bin ] ; then
    PATH=/usr/ant/bin:"${PATH}"
    export ANT_HOME=/usr/ant
fi

Start a new terminal and check that the command ant is accessible:

$ ant -version
Apache Ant version * compiled on *

Under Windows, check that the file env.bat defines the environment variable ANT_HOME and adds the folder C:\Program Files\Java\ant\bin to your PATH.

Add the XML file called build.xml to your project:

  1. <?xml version="1.0"?>
  2. <!-- +- Builds an application and an applet which display "Hello World!". -->
  3. <project name="Hello" default="help" basedir=".">
  4.  
  5.     <property name="appsrc" value="HelloWorldApp.java" />
  6.     <property name="appletsrc" value="HelloWorld.java" />
  7.  
  8.     <target name="help">
  9.         <echo message="Build file for section 'First Steps'" />
  10.         <echo message="Enter 'ant -projecthelp' for more help" />
  11.     </target>
  12.  
  13.     <target name="compile" depends="app, applet"
  14.         description="Compiles the application and the applet">
  15.     </target>
  16.  
  17.     <target name="app">
  18.         <javac srcdir="." includes="${appsrc}" deprecation="on" />
  19.     </target>
  20.  
  21.     <target name="applet">
  22.         <javac srcdir="." includes="${appletsrc}" deprecation="on" />
  23.     </target>
  24.  
  25.     <target name="test" depends="compile" description="Runs all the built test programs">
  26.         <echo message="Running the application..." />
  27.         <java fork="yes" classname="HelloWorldApp">
  28.         </java>
  29.         <echo message="To test the applet, load 'HelloWorld.html' in a navigator..." />
  30.     </target>
  31.  
  32.     <target name="clean" description="Deletes all generated files">
  33.         <delete>
  34.             <fileset dir="." includes="*.class" />
  35.         </delete>
  36.     </target>
  37. </project>

Try rebuilding the whole project:

$ ant clean test
clean:

app:
    [javac] Compiling 1 source file

applet:
    [javac] Compiling 1 source file

compile:

test:
     [echo] Running the application...
     [java] Hello World!
     [echo] To test the applet, load 'HelloWorld.html' in a navigator...

To list all the possible actions, pass the parameter -projecthelp to ant :

$ ant -projecthelp
Buildfile: build.xml

Main targets:

 clean    Deletes all generated files
 compile  Compiles the application and the applet
 test     Runs all the built test programs
Default target: help

Comments

To add a comment, click here.