How to Compile and Execute your 1st Java Program

numbers-1stIn this chapter you will learn:

JavaEditor – For writing colorful java code
Write your first java program
Compiling and executing Java Program

We assume that your PC is ready for java development. In this chapter we will write simple java program and will learn how to compile program using JVM and execute bytecode for getting output. But before writing java program, you must have a java editor to write program.

Where to write Java Program?

However, you can write your entire java program in notepad but I personally recommend that not to use notepad to write java program. The basic reason behind this is notepad is simple text editor and you will never get color coding as well as hierarchy coding view. It is very pathetic to develop java class in notepad.

There are lots of java editor are available freely on the net. You can download any of them. I am using DrJava as a java editor. It is free, light weight, easy to use and gives better view of java coding. You can download it from here.

http://www.drjava.org

DrJava

First Java Program

Step 1: Make a blank folder named JavaProgram in D drive or any other drive. This folder will contain your entire java program.

 

Step 2: Open DrJava Editor or notepad editor and write/paste following code.

 

class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Welcome to the world of Java");    
  }
}

Step 3: Save it with the name of HelloWorld.java

 

Note: Make sure to save your file with the same name of java class name. In the program we have created a class HelloWorld so we have saved this program with the name HelloWorld.java

Compile and Debug using command prompt

As you are using DrJava editor you can directly compile and execute your program but it is necessary to know how to compile and run your program using command prompt.

Step 1: Open command prompt

 

Step 2: Go to your folder location. Type D: and press enter. Now type cd JavaProgram and press Enter.

Java Path

Step 3: now type javac HelloWorld.java and press Enter button. If everything goes fine you won’t get any error message. It means your program is successfully converted into bytecode.

Compile Java

Step 4: Type java HelloWorld to execute your program.

Run Java Program

Note: When you compile your java program, it creates one more file with the same name of class but extension is .class.

class java

Compile and Debug using DrJava Editor

It is so much easy to compile and execute java program using this editor. There is dedicated button for compiling and executing your program. Let’s see how it works!

Dr Java Editor

Summary

In this chapter you have learned how to write and execute java program. You have also learned about java editor. In the next chapter you will learn some more java programming example. By doing all the question you will be familiar with java language.

Leave a Reply

Your email address will not be published. Required fields are marked *