Set Java Class Path and Run from Command Prompt

Why do you need ?

If you are interested to run your java programs manually by writing code in notepad then you are required to configure java CLASSPATH. Java run time environment needs to know the path of your code and additional libraries required to run the program successfully. To specify the path of your source code and additional java libraries to run the program you must have to set CLASSPATH.

Note : Modern IDE do not require to setup the CLASSPATH. The handle the path of source code and additional libraries. Depends which IDE you are using. These instructions are for manually configure the CLASSPATH.

Download JDK

Download and install latest version of JDK from here. Once completing the installation. Find the JDK directory structure, bin directory contains compiler and launcher e.g. java.exe, javac.exe and javadoc.exe.environment-directories

CLASSPATH

Windows XP

  1. Select Start, select Control Panel. double click System, and select the Advanced tab.
  2. 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.
  3. 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:

  1. From the desktop, right click the My Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced tab (Advanced system settings link in Vista).
  4. 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.
  5. 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:

  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. 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.
  5. 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.

SET Class Path from command Prompt

  1. Create a folder in directory of your choice. In my case, directory is c:\myjava
  2. Run Command Prompt . Type C:\> cd \myjava
  3. SET JDK path that will indicate where JDK is.  C:\myjava> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin   
  4. Open Notepad or other text editor
  5. Write the following program, name  Hello.java and save in c:\myjava
  6. Write the following command to compile your code (C:\myjava> javac Hello.java). javac has created Hello.class file and you can see this file among other files. This will not display any output but next command prompt.
  7. Write (C:\myjava java Hello) and this will run the java interpreter and you will see the program output.

Caution : If system don’t recognize javac command then check your JDK path. If you are facing errors then check your java program and trace the possible error.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.