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


Related pages


definition of a monologuedifference between gastric flu and food poisoningprotostomprove nounis dieing a wordwhat is micronutrients and macronutrientsproperties of alkanesimilarities between hinduism and buddhismdifference between apartment and townhousethe difference between cold blooded and warm bloodedwhat is one difference between primary and secondary successionis blackstrap molasses the same as molassesdefinition of monologue in dramastereotype in literature definitionhow to prepare projected balance sheetwhat is the lifespan of an american bulldogmeaning pailvulcanized rubber propertiessubordinating conjunctions definitioninternal and external rhymeexamples of polar and nonpolar substanceswhat is the difference between simple compound and complex sentencessnuggle and cuddledifference between a townhouse and condowhat is the definition of dicotdifference in bipap and cpapdifference between calorimeter and spectrophotometersilver nitrate test for halidesdifference between reflection and refractiondifference between ethics moralsspell tufftypes of animals herbivores carnivores omnivoreswhat is the difference between wineswhy are animals cold bloodeddifference between tensile strength and flexural strengthrelation between absorbance and concentrationorthopnoea definitionspore formation asexual reproduction definitiondifferences between a phrase and a clausejarrah propertiesaldose ketosewhat is tulsi plant called in english3 examples of heterogeneous mixturesthe difference between language and dialectintermolecular hydrogen bonding exampleswhat is tyndall effect explain with examplecarpe diem poetry exampleswhat does diploid and haploid meandifference between polymer and monomerwalnuts and pecansmacro and micronutrients definitionintercalary meristematic tissuechutney relish differencetumor vs neoplasmdefine functionalism psychologylevis redloop jeansdifference between typhoon and hurricanesmaid of honor meansdelhi to kullu manali trainlist of common nouns and proper nounsdistinction between language and dialectsummary of ugly ducklingdefinition uracildieing or dying meaningsolubility of sulphates in watervoltmeter and ammeter differencemonocot plant definitiondefine anode and cathodeporpoise and dolphin differencerolling friction physicswhat is difference between stack and queueidiopathic narcolepsydefinition of monologue in literaturemonocot vs dicot leaftooth plural formthemes in charlottes webprofession vs occupation