News

How to Set Classpath in Java

java classpath

Now you should have some basic concepts of Java programming. The classpath is an environment variable which tells the “javac.exe” compiler and “java.exe” interpreter where to find the Java program to compile to generate class file (byte code) and to interpret the byte code to native language of the corresponding machine in order to get desired output respectively. classpath only knows where predefined java packages, APIs (libraries) are, but if you are using any external libraries, then you have to explicitly set the classpath before or during compilation and execution of Java program.

Prerequisites

JDK must be installed in your machine. Let’s say it is installed in the path “C:\Program Files \Java\

Steps to set classpath

  1. Navigate to the path where JDK is installed. Further navigate to bin directory within JDK. In our case this is “C:\Program Files\Java\jdk1.6.0_14\bin”.
  2. Copy the path upto “bin” from address bar. In our case it is “C:\Program Files\Java\jdk1.6.0_14\bin”
  3. Go to My Computer. Then Right Click to get “Properties”.java classpath
  1.  After selecting “Properties”. Select “Advanced” tab. Screen will be like this:java classpath
  1. Then click on “Environment Variables” in order to get the screen given below:java classpath
  1. Now select the “Path” under System Variables. Now click on “Edit” button.java classpath
  1. Now you will find one small window where” Variable name:” will be “Path” and you have to append the path you have copied as per step 2 in the “Variable value:” field. You will find some paths already existing there.
    Caution: don’t delete the existing paths. Only go to the end of the existing string, put “;” if it’s not there. Then paste the copied path there, then put “;”. Press “OK” 3 times to complete the classpath setting.

Set Classpath for External API or JAR

If you are using any external API or jar, then you have to set classpath in either of the ways:

D:\javaprograms\example> set classpath=<list of full specified path of the APIs/jars separated by semicolon (”;”)>;

D:\javaprograms\example> javac Hello.java

D:\javaprograms\example> java Hello

OR,

D:\javaprograms\example> javac –classpath <list of full specified path of the APIs/jars separated by semicolon (”;”)> Hello.java

D:\javaprograms\example> java <list of full specified path of the APIs/jars separated by semicolon (”;”) Hello.

Now your classpath, for executing Java programs comfortably, has been set properly. Next we will see how to take command line arguments in a Java program.

Checkout more useful tutorials and definitive guidelines on Java programming here.

1 Comment

Leave a Reply

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

To Top