First java Program
The most common example of a first Java program is the famous Hello World! program. Before going to your first Java program you should download and install JDK (Java Development Kit) properly. The following link will guide you to download and install JDK on your system. Click here Downloading and installing JDK softwareWriting a Java hello world program
You should follow three steps to completely run your first Java program.
- Create a source file
- Compile the source file
- Run the program
Create a source file with .java extension
In your text editor (ex: Notepad in Windows) , create a new file and save it as "HelloWorld.java" . HelloWorld is your class name and you will need your class name to be the same name as your file. Copy the following source into your HelloWorld.java file.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Compile the source file into a .class file
A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. In order to compile your java file, navigate to the folder where you saved HelloWorld.java and type in javac HelloWorld.java.
C:\> javac HelloWorld.java
If there are no errors in your code, next step is to run the program.
Run the program
After successfully compile the HelloWorld.java, you will get a HelloWorld.class file in the same directory. In order to run your program, in the same directory, enter the following command at the prompt:
C:\> java HelloWorld
"Hello, World"
Just after you press enter key, you will get the message "Hello, World" in the next line. Congratulations! Your first Java program completed successfully!