Java Environment Setup

Compile and Run Java Program

To compile and run your Java program, you have to first setup environment for Java programming. You can setup environment for Java without any software, just by using your computer's command prompt. But this is time taking environment. It means if you compile and run your Java program using command prompt, this consumes your extra time for your each and every Java program.

So you can easily use Java compiler/IDE like Netbean or BlueJ to compile and run your Java program very easily. These two Java compiler/IDE are the most popular Java compiler/IDE.

Let's see how to setup environment using Netbean and BlueJ and also using command prompt(cmd).

Java Environment Setup - BlueJ

To compile and run your Java program using BlueJ, follow the following steps :

1. Download Java JDK from Internet and Install it.

2. Now download BlueJ from Internet and Install it.

3. After installing BlueJ, Open it.

4. The Opened BlueJ window will look like :

java environment setup

5. Now Click on the Project Menu then New Project present at the left top of the BlueJ window as shown in the following figure :

java bluej environment setup

6. Now Type Project Name like JavaProgram.java and click create or simply press enter as shown in the following figure :

compile and run java program

7. After performing this action, you will watch some changes in BlueJ window as shown in the following figure:

bluej

8. Now Click on the New Class Menu and Type Class name like JavaProgram then click OK button as shown in the following figure :

run java program

9. After performing this action, you will watch again some changes in BlueJ window as shown in the following figure:

compile java program using bluej

10. Now press double click on JavaProgram or make right click on JavaProgram and then click on Open Editor then a new Editor window will come out that will look like :

run java program using bluej

11. Now remove all the codes/texts that already available in the window and start typing your own Java program like :

public class JavaProgram
{
    public static void main(String args[])
    {
        
        System.out.println("Hello BlueJ, I am Java");
        
    }
}

as shown in the following figure :

bluej setup for java

12. Now press Compile button, now you will watch some message at the bottom of the window as shown in the following figure :

compile java program

13. If you will watch, no syntax errors, then close the window by pressing the close button

14. Now you will come to the back window, if you want to run and check your Java program's output, then right click on the class i.e. on JavaProgram and then click void main(String[] args), now a small window will come out like :

environment setup for java

15. Now press OK button to watch the output of your Java program, as shown in the following figure:

environment setup for java programming

Java Environment Setup - Netbean

To compile and run your Java program using Netbean, follow the following steps :

1. Download Java JDK from Internet and Install it.

2. Now download Netbean from Internet and Install it.

3. After installing Netbean, Open it.

4. The Opened Netbean window will look like :

java netbean setup

5. Now Click on File Menu then New Project present at the top left of the window, now a new window will come out as shown in the following figure :

compile and run java program using netbean

6. Now click on the Next button, then a new window again will come out as shown in the following figure :

run java program using netbean

7. Now, you can type your project name or simply leave it default name like JavaApplication1 and press Finish button, after pressing the Finish button, your project is created, and a new window will come out (i.e. editor window) as shown in the following figure:

netbean

8. Now you can type your Java programming code after the main() function i.e. below the // TODO code application logic here, you can also delete all the pre written codes/texts and write fresh code after removing unusable texts as shown in the following figure :

java netbean

9. Now type your Java program like :

public class JavaApplication1
{
    public static void main(String[] args)
    {
        
        System.out.println("Hello Netbean!, I am Java");
        
    }  
}

as shown in the following figure:

java program using netbean

10. After typing your Java program, you can compile by pressing F9 button and run by pressing F6 button or you can also compile your Java program by clicking on the run menu present at the top of the Netbean window. After compiling and running the above Java programming code, you will watch the output as shown in the following figure:

netbean java

Compile and Run Hello World Java Program using Command Prompt

Java's simplest program which would print the string "Hello World" as output.

/* Java Program Example - Java Environment Setup
*  This program prints "Hello World" */

public class JavaProgram 
{
    public static void main(String args[])
    {
   
       System.out.println("Hello World"); // prints Hello World
      
    }
}

Java Environment Setup - Command Prompt

To compile and run the above Java program using the command prompt, let's see how to do this.

Let's explain how to save the file, and compile then run the program using the text editor (Notepad):

Here is the output produced by the above Java program, after typing the first two line:

C : > javac JavaProgram.java
C : > java JavaProgram 
Hello World

Java Online Test


« Previous Tutorial Next Tutorial »