Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 1.6 Editing, Compiling, and Running a Java Program

In this section we give a brief overview of how to compile and run a Java program. For a more detailed discussion see Appendix B. We begin with a brief overview of the types of programming environments one might encounter.

Subsection 1.6.1 Java Development Environments

Java is owned and maintained by Oracle. It is available free for various platforms, including Linux, Windows, and macOS. Since 1998 Java has gone through many versions. The current version as of this writing is Java 18.
Everything needed to write Java programs — except for a text editor — is available for free in the Java Development Kit (JDK).
There are also several integrated development environments (IDEs) available for developing Java programs. IDEs integrate the JDK with a text editor, giving you everything you need in one software package. These include the open source Eclipse IDE and Oracle’s own NetBeans IDE.
Figure 1.6.1. Editing, compiling, and
Figure 1.6.1 illustrates the steps involved in creating and running a Java program. It assumes you are using the plain Java JDK as your development environment, not an IDE. The three basic steps are:
  1. Edit the Java source code using a text editor.
  2. Compile the Java source code into bytecode using javac.
  3. Run the Java bytecode using java.

Subsection 1.6.2 Editing a Program

Any text editor can be used to edit a Java program. Online development environments like are very popular. VSCode is a popular code editor that is available for Windows, Macs, and Linux systems. Popular Linux editors include vim and emacs, which are also available on macOS and Windows. Free macOS editors include BBEdit and TextEdit, which is also available for Windows computers. Notepad++ is also a popular Windows text editor. There are also Integrated Development Environments (IDEs) that include an editor, compiler, and other tools, such as Eclipse, Netbeans, IntelliJ, jGrasp, etc.
Let’s take another look at the HelloWorld program (Listing 1.5.1). After editing the HelloWorld program, it must be stored in a file named
The file name must exactly match the public class name. For example, if the file containing the HelloWorld class were named or, that would cause an error. Java would be looking for a file named

Subsection 1.6.3 Compiling a Program

Before you can run a Java source program you have to compile it into the Java bytecode, the intermediate code understood by the Java Virtual Machine (JVM). The compilation step converts Java source code into Java bytecode (Figure 1.6.1).
The Java compiler is named javac. It is included in the JDK. To compile, you would simply type the following command on the MacOS or Linux or Windows command line, where ’$’ represents the command prompt:
$ javac
Assuming there are no errors, javac will create a Java bytecode file named HelloWorld.class—a file that has the same name as the source file but with the extension class instead of java. By default, the bytecode file will be placed in the same directory as the source file. After compiling, the directory would look something like this:
If javac detects errors in the Java code, a list of error messages will be printed on the console —e.g.: error: ';' expected System.out.println(greeting) // Output statement ^ 1 error
In this case the error is a missing semicolon on line 11.

Subsection 1.6.4 Running a Java Program

Once the program successfully compiles and generates the HelloWorld.class bytecode file, it can be executed using the java command, also included in the JDK:
$ java HelloWorld Hello World!
As you see (Figure 1.6.4), the program prints “Hello World!” on the console. Note that we do not use the .class extension with the java command, just the file’s name.
Figure 1.6.4. Compiling and running the HelloWorld program.
The java command works as follows. It first loads the Java Virtual Machine (JVM) into the computer’s main memory. The JVM then loads the Java bytecode into memory and runs it.
Once a Java program is compiled it can be run on Windows, MacOS, Linux or any other computer system that supports the JVM. This platform independence is one of Java’s main strengths. Once compiled, Java bytecode is portable. This is not the case for other compiled languages, such as C and C++, which must be compiled separately for each different type of computer.
/** File: HelloWorldCanvas program */
import javax.swing.JFrame; // Import class names
import java.awt.Graphics;
import java.awt.Canvas;
public class HelloWorldCanvas extends Canvas // Class header
    // Start of body
    public void paint(Graphics g)  // The paint method
        g.drawString("Hello, World!", 10, 10);
    }  // End of paint

    public static void main(String[] args)   // The main() method
    {   HelloWorldCanvas c = new HelloWorldCanvas();
        JFrame f = new JFrame();
    }  // End of main
}  // End of HelloWorldCanvas
Listing 1.6.5. The HelloWorldCanvas program.
The steps involved in running the HelloWorldCanvas program (Figure 1.6.5) are identical:
$ javac 
$ java HelloWorldCanvas
In this case however, the program’s output would not appear on the command line. Instead, as shown in Figure 1.6.6, it would be displayed in a pop-up window, a Java JFrame.
Figure 1.6.6. Output from the HelloWorldCanvas program.

Activity 1.6.1.

Try the HelloWorldCanvas program below.
You have attempted of activities on this page.