next up previous
Next: From the source code Up: Unit 01 Previous: Second program

Subsections

Write, compile, and execute a Java program

  1. preparation of the program text
  2. compilation of the program
  3. execution of the compiled program

1. Preparation of the program text

To prepare the program text we have to write a file containing the program. For a Java program, the name of the file has to be

ClassName.java

where ClassName is the name of the class defined in the program. E.g., First.java

The program can be written with any program that allows one to write a text file (editor). E.g., NotePad, Emacs, ...

Example:

2. Compilation of the program

The compilation of the program is necessary to translate the program into a sequence of commands that can be directly executed by the computer. The standard Java compiler, which is part of the Java Standard Development Kit (Java SDK), is javac. To use it, you have to execute the command:

javac ClassName.java

The compilation produces as a result a file called ClassName.class, which contains the command that can be directly executed by the computer. For example:

javac First.java
creates the file First.class.

3. Execution of the compiled program

A program can be executed only after it has been compiled, i.e., when we have the file ClassName.class.

In Java the execution of a program is done through the command

java ClassName
(without .class). For example, the command

java First

causes the execution of the program First (or, more precisely, of the main method of the class First), and hence prints on the screen:

This is my first Java program.

Programming environment

There are applications called programming environments, that allow one to carry out the various steps of program writing, compilation, and execution in an integrated way. Examples of Java programming environments are: JavaONE, JBuilder, JCreator, ecc. The following picture shows a screen-shot of BlueJ, a programming environment developed for teaching purposes by the Monash University, Australia, e by the University of Southern Denmark.


next up previous
Next: From the source code Up: Unit 01 Previous: Second program