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.

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 “”.

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, “” 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.

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.

Related pages

difference between liberalism and neo liberalismwhat is the difference between carbohydrates and lipidssentence with predicate nominativedistinguish between metallic and nonmetallic mineralscomparison between spermatogenesis and oogenesisthe meaning of bewilderedwhat does foreshadow meanmeasuring absorbance using spectrophotometersynonyms dumbtooth teeth singular pluraldefinition of a bistroleast count of vernierwhat is the difference between bipolar and bpdresonance in series and parallel rlc circuitsdifference between atheist and agnosticdifferences between mendeleev and modern periodic tablemeaning of enunciateddefine closed syllablezoospores in algaemotto or sloganpatronising meaningwhat is the chemical formula of maltoseassociate and bachelor degree difference betweenhomophones for stationarywhat are the differences between nonrenewable and renewable resourcesrepose defdifference between alaskan and siberian huskydefinition thyminepaneer and tofuaddition condensation polymerizationantedote meaningexample of metonymy in literaturewhat is the difference between invention and innovationwhat is absorption costing with examplesmalinois colorsstationary and progressive waveswhat is the difference between a solution and a colloidwhat is difference between prokaryotes and eukaryotesmncs and tncsdifference between ranch and farmatypical and typical antipsychoticsmeaning of the prefix microosmolarity and tonicitydifference between attraction and affectionleukopenia etiologydifference between ethics and moralityalpha amylase definitiontypes of carrier proteinsangiosperm gymnospermthe difference between punishment and negative reinforcement is thatisle vs aislewhat is the difference between autotrophic and heterotrophic nutritionbyte vs biteelectric force vs magnetic forcehow to make resonance structuresdifference in hurricane and typhoonemigrants vs immigrantscytokinesis refers tocentripetal and centrifugal forcedifference between receptive and expressive languagewhat is the difference between distance and displacement in physicshoney badger vssinus cysts and polypsdefinition of haiku poemsoviet union flag cold wardefinition for sliding frictionflying ants south africameiosis and mitosis differenceallylic hydrogendifference between vascular and non vascularevaporative distillationwhat is the difference between chef and cooksiberian husky and malamute