Creating, Compiling, and Executing a Java Program

In this article, we will learn how to create a simple Java program and then we will compile it and finally, we will execute the compiled program.
Basically, we save a Java program in a .java file and compile it into a .class file. The .class file is executed by the Java Virtual Machine.
You can use any text editor or IDE to create and edit a Java source-code file. In this article, I demonstrate how to create, compile, and run Java programs from a command window.
Let's use command window and open a text editor such as Notepad to create the Java source-code file Welcome.java.

Welcome.java

Let's create a Java program and name this file to Welcome.java:
public class Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
   }
}
A Java compiler translates a Java source file into a Java bytecode file. 
The following command compiles Welcome.java:
javac Welcome.java
If there aren’t any syntax errors, the compiler generates a bytecode file with a .class extension. Thus, this command generates a file named Welcome.class.
To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated.
The following command runs the bytecode:
java Welcome
The output of the above program displays the message “Welcome to Java!”

Java program-development process

The below diagram demonstrates the above Java program development process consists of repeatedly creating/modifying the source code, compiling, and executing programs.

Key points

Caution 

Do not use the extension .class in the command line when executing the program. Use java ClassName to run the program. If you use java ClassName.class in the command line, the system will attempt to fetch ClassName.class.class

Tip

If you execute a class file that does not exist, a NoClassDefFoundError will occur. If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main), a NoSuchMethodError will occur. 

Note 

When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called the bytecode verifier to check the validity of the bytecode and to ensure that the bytecode does not violate Java’s security restrictions. Java enforces strict security to make sure that Java class files are not tampered with and do not harm your computer.

Summary

  • The Java source file name must match the public class name in the program. Java source code files must end with the .java extension.
  • Every class is compiled into a separate bytecode file that has the same name as the class and ends with the .class extension.
  • To compile a Java source-code file from the command line, use the javac command.
  • To run a Java class from the command line, use the java command.
  • Every Java program is a set of class definitions. The keyword class introduces a class definition. The contents of the class are included in a block.
  • A block begins with an opening brace ({) and ends with a closing brace (}).
  • Methods are contained in a class. To run a Java program, the program must have a main method. The main method is the entry point where the program starts when it is executed.
  • Every statement in Java ends with a semicolon (;), known as the statement terminator.
  • Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program.
  • In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. Comments are ignored by the compiler.
  • Java source programs are case sensitive.
  • Java bytecode can be executed on any computer with a Java Virtual Machine.
Read more about how to create Java programs with more examples at Java First Hello World Program.

Comments