Java Environment Variable Setup - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Java Environment Variable Setup

Share This

Before starting with the well-known Hello World! program, first we have to setup development environment. If you are familier with C programming language, probably you know that C programs can be compiled on a machine, if compiler is installed.

If you are using Linux or Unix systems, probably GNU C compiler is pre installed. Otherwise, you have to install one. If you are using Windows environment, probably you have installed Turbo C or any other compiler cum editor.

In the similar way, before writing any Java program, you have to configure your system. First, we will discuss how to setup Java Development Kit (JDK) on Windows system, followed by JDK setup on Linux environment.



Environment setup on Windows platform


To compile a Java program, first, you have to install Java Development Kit (JDK) on your system if JDK is not installed. So, download and install JDK on your system.

After this installation, you will not be able to compile a code from any location, because system PATH variable is not configured. At this stage a java program can be compiled from withinbin directory of JDK. Hence, to avoid this, we have to configure it, so that a Java program can be compiled from any location of the system. Thus, you may follow the steps given below.

Right click on This PC, click on Properties.

Then click on Advanced system settings, a new window will appear by name System Properties. Click on Advanced tab.

Click on Environment variables, another window will appear.

Click on Path in System Variables, it will be selected. Then click on Edit and the following window will appear. Click on new and add the path where JDK is installed (upto bin directory as highlighted).

If you have opened any terminal before configuring this PATH, close the terminal and reopen it.


How to write the first program in Java?

Now, you can try to write the first program. Instead of using object-oriented concepts, we are writing a basic program to know how a program will be compiled and executed.

Write the following code and save the content in a file with .java extension.

Though the file can be saved by any name with .java extension, you may use TestMain.java as best practice.

public class TestMain {

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

Now compile the code using javac utility as follow.

$javac TestMain.java

If compilation is successful, a TestMain.class file will be generated. The .class file is a Byte Code. It will not run on systems where Java Runtime Environment is not installed.

Run the program using java utility.

$java TestMain


Happy Exploring!

No comments:

Post a Comment