About NewTechnoBuzz
Advertise
Contact Us

Monday, July 7, 2014

How to set Class path for Java in Windows and Linux

Sometimes, we need to configure java to run on our computer. This article explains how to use the PATH and CLASSPATH environment variables on Microsoft Windows, Solaris, and Linux.
After installing the software, the JDK directory will have the structure shown below.

JDK Folder Structure

The bin directory contains both the compiler and the launcher.

Update the PATH Environment Variable (Microsoft Windows)

Set the PATH environment variable if you want to run the executables (javac.exe, java.exe, javadoc.exe etc.) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:\Java\jdk1.6.0\bin\javac TestMyClass.java

It is useful to set the PATH environment variable permanently so it persists after rebooting because it's little problematic if we have to provide full path to the executable every time. To make a permanent change to the PATH variable, use the System icon in the Control Panel. The precise procedure varies depending on the version of Windows:

Windows XP

  • Select Start, select Control Panel. Double click System, and select the Advanced tab.
  • Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Windows Vista

  • From the desktop, right click the My Computer icon.
  • Choose Properties from the context menu.
  • Click the Advanced tab (Advanced system settings link in Vista).
  • Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Windows 7

  • From the desktop, right click the Computer icon.
  • Choose Properties from the context menu.
  • Click the Advanced system settings link.
  • Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.


Note: Click here to see the steps for setting the PATH variable in Windows.

Update the PATH Variable (Solaris and Linux)

You can run the JDK without setting the PATH variable, or you can optionally set it as a convenience. However, if you set the PATH variable then it'll be good for later. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it which is little problematic, such as:

% /usr/local/jdk1.6.0/bin/javac TestMyClass.java

To find out if the path is properly set, rule below command on console:

% java -version

This will print the version of the java, if it can find it. If the version is old or you get the error java: Command not found, then the path is not set properly.
To set the path permanently, set the path in your startup file.
For C shell (csh), edit the startup file (~/.cshrc):

set path=(/usr/local/jdk1.7.0/bin $path)

For bash, edit the startup file (~/.bashrc):

PATH=/usr/local/jdk1.7.0/bin:$PATH
export PATH

For ksh, the startup file is named by the environment variable, ENV. To set the PATH for Java:

PATH=/usr/local/jdk1.7.0/bin:$PATH
export PATH

For sh, edit the profile file (~/.profile):

PATH=/usr/local/jdk1.7.0/bin:$PATH
export PATH

Then load the startup file and verify that the path is set by repeating the java command:

For C shell (csh):

% source ~/.cshrc    
% java -version

For ksh, bash, or sh:

% . /.profile     
% java -version


I hope that you have got a good understanding on how to set PATH for Java environment. If you have any comment or feedback, please share with us.

0 comments