linuxdot.org Linux Expo Birmingham 2002
Linux news | Newbie's Linux manual | Linux links | Link us
The Linux columns | Book reviews
  DistroWatch + TuxReports October 28, 2002

Linux Java intro

Stewart Hepburnby , 27 November, 2001

It's programming tradition that the first program you do in any language, is to display, "Hello, World!" To do this you must first have the Java SDK (Software Development Kit) installed. It's included on some Linux distributions, but not all. To find out if you have it installed enter javac --help at the terminal.

If a "command not found" message appears, you'll have to install it. The Java 2 SDK, Standard Edition, version 1.3.x for Linux (Intel x86) in tarball and RPM format you can download, here (24.3Mb).

Your next task, is to decide on which text editor to code in. Glimmer comes recommended for its syntax highlighting, tabs to navigate multiple documents, and line numbers to help track down coding errors.

Your first Java program

Once you have decided on a text editor, create a file called HelloWorld.java, and enter the following code, making sure the HelloWorld part of the filename and the HelloWorld class name are of the same case:


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

To compile your program, enter javac HelloWorld.java at the terminal. No error messages indicate the file HelloWorld.class has successfully been created. If errors do appear, check your code carefully for typos.

To run your program, and display "Hello, World!", enter java HelloWorld.

Your second Java program

Here's a slightly more advanced program illustrating the for loop, to display the numbers 0 to 9. Enter it and save as ForExample.java:


class ForExample
{
        public static void main (String[] Args)
        {
                for (int i = 0; i < 10; i++)
                        System.out.println(i);
        }
}

To compile it, enter javac ForExample.java, and to run, enter java ForExample.

For further information

This column served only to show you how simple Java programming in Linux can be. For further tutorials check out the links below:

About us  
Latest stable kernel: 2.4.19 | Latest development kernel: 2.5.44
Copyright © 1998-2002 Linuxdot.org.
Linux ® is a registered trademark of Linus Torvalds.