Program Structure & Environment
Home ] Up ] [ Program Structure & Environment ] Comments ] Primitive Data Types ] Reference Data Types ] No Preprocessor ] Typesafe enums ]

 

 

A very simple Java application looks like this:

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

Program Structure

Every Java program contains a number of classes

A class has:

  • a name (here, HelloWorld)
  • some number of methods
  • some number of class variables
A Java application must have at least one main class, which has:
  • a method called main with the following signature:
	public static void main(String[] args)
This means that it:
  • has a single argument of type array of String
  • returns void -- i.e., nothing
When you run an application, you specify the name of a main class.

If that class doesn't have a main method with the proper signature -- Error!

Objects, Objects

Everything in a Java program is an object (except for the primitive data types).

In the above HelloWorld example, the main method's body calls the println method of the System.out object.

Command Line Arguments

The single argument to main contains all the command line arguments

Unlike C and C++, the command line arguments do not include the program name

public class EchoArguments
{
    public static void main(String[] args)
    {
        System.out.print(args.length);
	System.out.println(" arguments passed:");
        for (int arg = 0; arg < args.length; arg++)
            System.out.print("'" + args[arg] + "' ");
        System.out.println("");
    }
}

If the above program is run as follows:

java [options] EchoArguments fee "fi fo" fum 12 Englishmen at 0.5 apiece

will output:

8 arguments passed:
'fee' 'fi fo' 'fum' '12' 'Englishmen' 'at' '0.5' 'apiece'

Note that, normally, command line arguments are separated by whitespace.  However, you can enclose a single argument within quotes to override this behavior.

Program Exit Value

If you wish to return a numeric value to the system, as a normal C or C++ program would, you must call the System.exit method:

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

Environment:

Java does not support operating system environment variables -- that's non-portable

However, Java does support a platform-independent system properties list:

public class PropertiesDisplayer
{
  public static void main(String[] args)
  {
    String home = System.getProperty("user.home");
    System.out.println("HOME = " + home);
    String classPath = System.getProperty("java.class.path");
    System.out.println("CLASSPATH = " + classPath);
    System.getProperties().list(System.out);
  }
}

When I ran the above program on my system (from within NetBeans), it gave me the following output:

HOME = C:\Documents and Settings\Bryan Higgs
CLASSPATH = C:\Documents and Settings\Bryan Higgs\My Documents\Java Projects\Test\build\classes
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0\jre\bin
java.vm.version=1.5.0-b64
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\Documents and Settings\Bryan Higgs...
java.runtime.version=1.5.0-b64
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0\jre\li...
os.arch=x86
java.io.tmpdir=C:\DOCUME~1\BRYANH~1\LOCALS~1\Temp\
line.separator=
java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk1.5.0\bin;.;...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\Bryan Higgs
user.timezone=
java.security.policy=applet.policy
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.5
user.name=Bryan Higgs
java.class.path=C:\Documents and Settings\Bryan Higgs...
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.5.0\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0
java.ext.dirs=C:\Program Files\Java\jdk1.5.0\jre\li...
sun.boot.class.path=C:\Program Files\Java\jdk1.5.0\jre\li...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...

As you can see, even the default system properties provide quite a bit of information.

You can also pass name/value pairs into the Java program by using the -D option on the command line.

 

This page was last modified on 02 October, 2007