Compile a Java Program
Overview
Let’s see the steps to compile a Java program in this tutorial. We will follow the beginner’s steps and approach to compile a given Java program.
Prerequisites
- JDK
- A text editor or IDE tool.
- Java Program source code.
JDK
Make sure you have the JDK (Java Development Kit) installed on your machine. The JDK bin directory consists of the javac compiler. ( javac.exe on Windows)
Go to the command prompt using the Start menu
Programs >> All Programs >> Accessories >> Command prompt.
You can check if it’s installed by opening a command prompt and running the following Java command:
\> java -version
Text editor
A text editor or an IDE tool to write the source code. For example, Notepad on the Windows operating system.
Java program
Source code of the Java program to compile. A program is a set of programming statements using the Java programming language. In this example, we will write a simple Java program to print a Hello World message.
// Java Program to print Hello World message public class HelloWorld {     public static void main (String args[]{    System.out.println ("Hello World!.");     } }
The Java program prints the message “Hello World !.” when you compile and run it.
- Source code in Java is essentially written inside a class.
- We have created a class HelloWorld to put the main() method inside it. The main() method is the entry point of the Java application.
- The main() method must be declared as static. It must return nothing (void) and takes an array of type String as an argument.System.out.println() is used to print the given message.
Compile a Java Program
To compile a Java program, follow the below steps:
Add the Java code. Type the Java program using a text editor like Notepad and save the file name with the .java file extension.
If you are using Notepad, enclose the filename in quotes (“HelloWorld.java”) while saving it, otherwise, Notepad gives the default text file extension .txt.
Compile Java program using javac.exe as follows:
 \> javac HelloWorld.java
This will compile your Java code and generate a bytecode file named “HelloWorld.class” if there are no compilation errors.
In case of any compilation errors, you need to debug the Java program and resolve the errors. You need to recompile the Java code.
Common error:
The common error when you try to compile a Java program is the following error:
‘javac’ is not recognized as an internal or external command,
operable program or batch file.
Set the PATH environment variable of Windows to the bin directory of JDK as follows at the command line. PATH is required as javac.exe and java.exe in the bin directory
For example,
\> SET PATH=C:\Program Files\Java\jdk1.5.0_22\bin
The PATH environment variable informs the Windows operating system where it should search in the file system for executable files. By default, OS searches for executable files only in the current directory. To see the current PATH, type the following command at the prompt.
\> PATH
\> echo %PATH%
Run the program
Run the program using the following command.
\> java HelloWorld
After the compilation, you can run the Java program using the java command followed by the class name without the .class file extension.
That’s it! You’ve successfully compiled and executed a simple Java program.
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :