How to Write a Simple Java Program

The professional java developers usually use an IDE (Integrated Development Environment) such as Eclipse, NetBeans, etc. to write java programs. But you can even use a notepad to write a simple java program, if you have the required software installed in your machine.

Setting up Development Environment to Write a Simple Java Program

First of all, we need to set up the development environment for java development. You can follow the two steps procedure given below to set it up.

Step 1: Install Java Development Kit (JDK)

JDK includes the Java Runtime Environment (JRE), Java Compiler (javac), Java Archive (JAR), and Java Documentation generator (Javadoc), which help you develop java applications on top of Windows or Linux platform.

You can download JDK from the following link.
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 2: Set the Path of JDK

After installing JDK. You need to add the location of the JDK bin directory to the “PATH” variable under “User Variables” in the (Windows) system. Below is how you can do it.

• Open up the wizard, system properties by selecting My Computer -> Right Click -> Properties -> Advanced system settings.

• The “Advanced” tab will automatically be selected.

• Click on the button, “Environment Variables” at the bottom.

How to write a simple Java Program_Set the path of JDK

• You will be displayed the wizard, “Environment Variables”.

• Select the “PATH” variable listed under “User Variables” and click on the “Edit” button.

How to write a simple Java Program_Edit the Path Variable

• Add the path of the JDK bin directory to the “Variable value” of the “PATH” variable.

How to write a simple Java Program_Edit User Variable

• Click on the “OK” buttons in both “Edit User Variables” and “Environment Variables” wizards.

• Open up a command prompt by selecting Start menu -> All Programs -> Accessories -> Command Prompt.

• Type the command, “path” to check whether the changes have been applied. You should be able to see that the path to JDK bin directory is assigned to the “PATH” variable.

How to write a simple Java Program_Check Changes  Applied

Write a Simple Java Program

The step by step procedure given below will guide you in writing a simple java program to print the sentence, “This is a simple java program.” using a notepad. You will use the command prompt to compile and run the program.

Step 1: Open up Notepad

Open up a notepad by selecting Start menu -> All Programs -> Accessories -> Notepad.

Step 2: Write Class Name

Write a class with the name “MyJavaProgram”.

How to write a simple Java Program_Step 2 - Write Class Name

Java Keywords:

class is used to declare a class in java.

Step 3: Set Class Visibility

Set the class visibility to public.

How to write a simple Java Program_Step 3 - Set Class Visibility

Java Keywords:

public is an access modifier to set the visibility of the class to all levels.

Step 4: Write Main Execution Method

Inside the class, write the main method to be executed when the application runs.

How to write a simple Java Program_Step 4 - Write Main Execution Method

Java Keywords:

main indicates the startup method of the java program.
public is used to set the visibility of the main method to all levels.
static is used to indicate that the method belongs to the class not to an instance of it. Here, the main method is set to static so that it can be directly invoked via the class as it is being called by the JVM before any object is created.
void indicates the return type which does not return any value. As we plan only to print a sentence using the main method, the return type is given as void.
String [] args is an array of string arguments passed to main method.

Step 5: Write Print Statement

Inside the main method, write the print statement to print the sentence “This is a simple java program”.

How to write a simple Java Program_Step 5 - Write the print statement

Java Keywords:

System.out.println() is the method used to print the data values passed to it. Here, we pass the string “This is a simple java program” to this method to print it in the command line.

Step 6: Save Notepad

Save the notepad as “MyJavaProgram.java”.

How to write a simple Java Program_Step 6 - Save Notepad

Now you are done with writing the code. Let’s compile and run the application.

Step 7: Compile the Program

Open up a command prompt by selecting Start menu -> All Programs -> Accessories -> Command Prompt.

Step 7.1: Go to the File

Move to the location where you saved the file, “MyJavaProgram.java” using the following command.
cd <relative path to the location of the java file>

How to write a simple Java Program_Step 7.1 - Compiling the Program

Step 7.2: Compile

Compile the java program using the following command.
javac MyJavaProgram.java

How to write a simple Java Program_Step 7.2 - Compiling the Program

Step 8: Run the Program

Run the java application using the following command. This will print “This is a simple java program” in the command prompt.
java MyJavaProgram

How to write a simple Java Program_Step 8 - Run the Java Program

About the Author: Isha

Isha is a Senior Software Engineer, currently working for a global software development and service company. She has contributed in design and development of commercial mobile applications for over 4 years. She works as a freelance writer to fulfill her passion as a writer, so that she can share her knowledge, ideas and findings with others of similar interest.