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:
- @echo off
- set PATH=%SystemRoot%\system32;%SystemRoot%
- echo Setting environment for Java (PATH JAVA_HOME).
- set JAVA_HOME=%ProgramFiles%\Java\jdk
- set PATH=%JAVA_HOME%\bin;%PATH%
- echo Setting environment for Ant (PATH ANT_HOME).
- set ANT_HOME=%ProgramFiles%\Java\ant
- 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:
- public class HelloWorldApp {
- public static void main(String[] args) {
- System.out.println("Hello World!");
- }
- }
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:
- import java.applet.Applet;
- import java.awt.Graphics;
- public class HelloWorld extends Applet {
- public void paint(Graphics g) {
- g.drawString("Hello world!", 50, 25);
- }
- }
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:
- <html>
- <head>
- <title>Hello Applet</title>
- </head>
- <body>
- <applet code="HelloWorld.class" width="150" height="25"></applet>
- </body>
- </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:
- // <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:
- Manifest-Version: 1.1
- 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:
- Manifest-Version: 1.1
- 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:
- <html>
- <head>
- <title>Hello Archive</title>
- </head>
- <body>
- <applet archive="HelloWorldApp.jar" width="150" height="25"></applet>
- </body>
- </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:
- <?xml version="1.0"?>
- <!-- +- Builds an application and an applet which display "Hello World!". -->
- <project name="Hello" default="help" basedir=".">
- <property name="appsrc" value="HelloWorldApp.java" />
- <property name="appletsrc" value="HelloWorld.java" />
- <target name="help">
- <echo message="Build file for section 'First Steps'" />
- <echo message="Enter 'ant -projecthelp' for more help" />
- </target>
- <target name="compile" depends="app, applet"
- description="Compiles the application and the applet">
- </target>
- <target name="app">
- <javac srcdir="." includes="${appsrc}" deprecation="on" />
- </target>
- <target name="applet">
- <javac srcdir="." includes="${appletsrc}" deprecation="on" />
- </target>
- <target name="test" depends="compile" description="Runs all the built test programs">
- <echo message="Running the application..." />
- <java fork="yes" classname="HelloWorldApp">
- </java>
- <echo message="To test the applet, load 'HelloWorld.html' in a navigator..." />
- </target>
- <target name="clean" description="Deletes all generated files">
- <delete>
- <fileset dir="." includes="*.class" />
- </delete>
- </target>
- </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