This blog is under construction

Saturday, 19 March 2016

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following.
  • How to install JDK
  • How to run java program without setting java path
  • How to set temporary java path in windows
  • How to set permanent java path in windows

How to install JDK?
  • Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website.
  • Install the downloaded JDK in your PC.

Once JDK is installed, you can run your java program using any method shown below.
  • Having your java source code under jdk/bin, compile the source code and run the program from same directory
  • Setting temporary path for java, compiling the source code and running the program from any location using command prompt
  • Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt

Let us see how to run java program without setting environmental variables.  Once java is installed in your PC, move to the location where java is installed and check whether javac and java executable are present(jdk/bin).  In the below example, Java is installed in the below path.

C:\Java\jdk1.6.0_22\bin


I have a java program named javaexample.java stored under C:\Java\jdk1.6.0_22\bin directory where java compiler and java executable or application launcher are present. Now, let us try to compile and execute our java program.

Launch the command prompt and move to the location where javac.exe and java.exe are located(C:\Java\jdk1.6.0_22\bin).  Then, compile the java source code as shown below.

javac  javaexample.c

where javaexample.c is the file where our source code resides.  As a result of compilation, we will get a class file named javaexample.class under the same directory where the source code is located.



Below is the source code of the program which we compiled above.  And the program would print "Hello World" on execution.

public class javaexample {

    public static void main(String args[]) {
        /* prints "Hello World" on the output screen */
        System.out.println("Hello World");
    }

}

Let us execute the program using java application launcher and check the output.

"Hello World" is printed on our output screen as a result of executing our java program.

How to set temporary java path in windows?
Below are the steps to set the temporary java(JDK) path to compile and execute your java program that is located anywhere in our computer.
  • Command prompt would display javac as unrecognized command when the same is executed without setting path environmental variable.
  • Set the path environmental variable with the location where java compiler(javac) and java application launcher(java) are located.  In my case, javac and java are located in C:\Java\jdk1.6.0_22\bin.  Hence, I am setting the same location as value for path environmental variable in command prompt.  This tells our system about the location of javac and java executable.
  • Run javac in command prompt again and check whether the command is recognized.

Please note that the above set path would be cleared on closing the command prompt. We need to set the path again if we need to compile or execute the program after relaunching command prompt.

How to set permanent java path in windows?
Below are the steps to set permanent java(JDK) path to compile and execute your java program that is located anywhere in our computer.
  • Click Start -> Control panel -> System and Security -> System
  • Click Advanced settings which will open a pop up window named "system properties"
  • Choose "Advanced" tab in system properties
  • Click on "Environment variables" and update your Java(JDK/bin) path.
  • Run javac in command prompt again and check whether the command is recognized.
Trying to execute javac in command prompt without setting path environment variable.

Trying to set the environment variable(Start -> Control panel -> System and Security -> System -> Advanced system settings).

Setting environment variable - Click new under user variable and update PATH variable and its value.  Please don't update system variable.

Checking whether javac command is getting recognized in command prompt.


2 comments:

  1. your Java concepts are really very well nice thus it is very well done and i got more interesting information it is very much useful too.


    cognos Training in Chennai

    ReplyDelete