Section 1.6 Editing, Compiling, and Running a Java Program
In this section we give a brief averview 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 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:
Edit the Java source code using a text editor.
Compile the Java source code into bytecode using
javac
.Run the Java bytecode using
java
.
Subsection 1.6.2 Editing a Program
Any text editor can be used to edit a Java program. 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.
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 HelloWorld.java
.
Principle 1.6.2. File Names.
A text file that defines a public
Java class named ClassName
must be saved in a text file named exactly ClassName.java
. There can be only one public
class per file.
The file name must exactly match the public class name. For example, if the file containing the HelloWorld
class were named helloworld.java
or Helloworld.java
, that would cause an error. Java would be looking for a file named HelloWorld.java
.
Principle 1.6.3. Case Sensitivity.
Java is case sensitive, which means that it treats helloWorld
and Helloworld
as different names.
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 HelloWorld.java
, you would simply type the following command on the MacOS or Linux or Windows command line, where '$' represents the command prompt:
$ javac HelloWorld.java
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:
HelloWorld.class
HelloWorld.java
If javac
detects errors in the Java code, a list of error messages will be printed on the console āe.g.:
HelloWorld.java:11: 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.
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.
The steps involved in running the HelloWorldCanvas
program (FigureĀ 1.6.5) are identical:
$ javac HelloWorldCanvas.java
$ 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
.