The System Administrator Notebook

Pages in the Java section

Java Syntax Java Classes

The Java Programming Language

Page Contents

To develop in Java download the Java Devolopment Kit from Sun

With the JDK installed, Java programs can be created using a text editor.

Console "Hello World"

Top Bottom

Create a 'HelloWorld.java' file with a text editor and add the following text:

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

The source file can be compiled into javabytecode using:

 javac HelloWorld.java

Use '-d' to specify an alternate output path for the compiled class.

Compiling the source code will produce a HelloWorld.class file, which can be run using the Java VM, as follows:

java HelloWorld

'System property' values can be passed on the command line using the -D switch and are accessed within the application using the System.getProperty() method

java -Duser=david -Dgreet=friendly HelloWorld

The javap command can be used to list methods and properties for a class:

javap HelloWorld

javap can print information about any class that is in the classpath, the current directory or part of the core Java system. The CLASSPATH environment variable specifies additional locations that the java searches for class files. The core classes do not need to be included in the CLASSPATH. The CLASSPATH can be specified to search both directories and jar files:

CLASSPATH=/home/david/java/classes:/home/david/ProjectX/app.jar:.
export CLASSPATH
...
set CLASSPATH=d:\users\david\classes;d:\users\david\ProjectX\app.jar;.

The classpath can also be specified using the -classpath switch. If no CLASSPATH is specified, java will use the current directory.

GUI "Hello World"

Top Bottom

To create a 'Hello, World' program in Java using Java's Swing graphical toolkit, enter the following text into a HelloWorld.java file:

import javax.swing.*;

public class HelloWorld
{
	public static void main( String[] args) {
		JFrame frame = new JFrame("Hello World");
		JLabel label = new JLabel("Hello, World");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(label);
		frame.setSize(300, 300);
		frame.setVisible(true);
	}
}

Executable Jar Files

Top Bottom

The jar utility can be used to create or extract Java Archive files, and works similarly to the Unix tar command:

Create a jarfile
jar -cvf myapp.jar path [path] [...]
Extract the contents of a myapp.jar
jar -xvf myapp.jar [path] [...]
List the contents of a myapp.jar
jar -tvf myapp.jar [path] [...]

A JAR file, allows you to package the files for an application in one compressed file for distribution. The jar file can be run by the JVM using the -jar option:

java -jar myapp.jar

During the creation of the JAR, a META-INF directory is added to the jar with a manifest file containing various option:value pairs to define attributes of the archive. One important attribute is the 'Main-Class' option. By adding this to the MANIFEST.MF, the JAR file can be made executable in GUI environments. To add the 'Main-Class' attribute to the manifest for the archive:

  1. Create a text file with the 'Main-Class' entry. The text file should have an blank line at the end. That is if the class containing the main method is call myapp.class the text file should look like this:
    Main-Class: myapp
    
    
  2. create the jar file specifying -m in the options to jar
    jar -cvfm myapp.jar manifest.txt path [path] [...]
    The name of the JAR archive to be created and the manifest text file must be given in the same order as the -f and -m options are specified to the jar command

Runnable JAR archives (those with Main-Class specified in MANIFEST.MF) are executable by double-clicking the jar file in GUI environments

As an alternative to using a text file to create the 'Main-Class' entry in the MANIFEST.MF, you can specify the -e option (entry point) to jar, which allows you to specify the class that contains the main method:

jar -cvfe myapp.jar myapp path [path] [...]

Once again, the JAR file to create and the name of the class containing the main method must be specified in the same order as the -f and -e options.