Wednesday, November 20, 2013

# 4 Why to set Java Path?

Hello,

In my previous post, we discussed how to compile/run java programs from C:\Program Files\Java\jdk1.5.0_22\bin folder. In this post you are going to learn how to execute java programs from any folder in the system. In other words, we will learn how to set the path of Java globally.

Path is nothing but a building path of Java where you can find all the Java library files which are needed to compile and run your programs/applications.
There is also another one named Classpath which need to be set for running the Java Applications. As we are in the beginning level, we will discuss more about Classpath in future posts.
Let us start with experiments for better understanding.
  1. Copy a java file (say in our case LindlarsForLife.java ),  on your desktop. Now open your command prompt and navigate the command to the Desktop folder by using “cd” command. In my case,
    C:\Documents and Settings\Administrator>cd C:\Documents and Settings\Administrator\Desktop
    gives me,C:\Documents and Settings\Administrator\Desktop>
  2. Now try to compile your java program from Desktop folder.

    C:\Documents and Settings\Administrator\Desktop>javac LindlarsForLife.java
    When you hit enter, you will get an error as shown below,

    'javac' is not recognized as an internal or external command,
    operable program or batch file. 
  3. At this point we will be wondering, why javac command is not recognized when we have installed Java in the system.
  4. We know that the same javac command was worked when we execute the same java program from the following folder,

    C:\Program Files\Java\jdk1.5.0_22\bin

So here the question is WHY? As I had already mentioned, my blog concentrates on WHY rather than HOW..!!

Why java commands are recognized in one folder but not in other.
The answer is simple; the Java path was not set globally, if you try to execute java commands outside the Java installation folder the java commands are not recognized and the Operating System (O.S) doesn’t understand what you are trying to do.
To overcome this, we need to set the path variable of the O.S to make it understand java commands. This can be done in 2 ways.
  1.        Using Command prompt
  2.        Using My Computer --> Properties

Setting the Java path using command prompt is a temporary process. Whenever you reboot your system, the path will be removed and you need to set it again.
You can set the path in command prompt by using path command as shown below.

C:\Documents and Settings\Administrator>set path=C:\Program Files\Java\jdk1.5.0_22\bin

Setting the Java path using My Computer --> Properties is a permanent process. I suggest you to follow this so that you need not necessarily set it every time you restart your system.

To know more about how to set the Java path in Windows Operating System, you can find the clear details here.

Make sure that you do not touch any of the already existing variables in the Path. Deleting/Modifying any of the existing variables in the Path will affect your systems working environment.


So if you have successfully added the Java Path to the System Environment Variables, now you should be able to compile/run the Java programs from any folder of your system.

You can test this by again trying to compile/run the LindlarsForLife.java from your desktop. This time, you should get the expected output.

Hit Points:
  1. We set the Java Path to tell our system where to find the Java commands so that you can do your programming from anywhere in your system.
  2. Setting the path in command prompt is a temporary process.
  3. Setting the path in My Computer properties is a permanent process.


Thanks. :)

0 comments:

Post a Comment