Search

Set Path and Classpath | Difference between Path and ClassPath



Set Path and Classpath in Java

Hello friends here we learn about Set Classpath in java and also learn about Difference between path and classpath.

PATH

The following programming error is general for all java programmers when they compile any java program.

‘javac’ is not recognized as an internal or external command, operable program or batch file.

When you get this type of error, then your operating system cannot find the java compiler (javac). To solve this error you need to set the PATH variable.

Javac is a tool which is available in bin folder so you must set the PATH upto bin folder.

In a bin folder all tools are available like javap, javah, jar, javac, java, appletviewer etc. These all tools are used for different-different purpose.

How to set the PATH variable

Steps for set the PATH

1. Click on Start button > right click on ‘my computer’ > properties

2. Click advanced > environment variable

Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:

C:\jdk<version>\bin (jdk<version is nothing but the name of the directory where jdk is installed)

“C:\Program Files\Java\jdk1.6.0\bin” when we put .; (dot semicolon ) at end then it select all the tools which is present in bin folder.

If there are already some entries in the PATH variable then you must add a semicolon and then add the above value (Version being replaced with the version of JDK). The new path takes effect in each new command prompt window you open after setting the PATH variable.

Set Classpath in Java

Exception in thread “main” java.lang.NoClassDefFoundError:HelloWorld
If you receive this error, java cannot find your compiled byte code file, HelloWorld.class. If both your class files and source code are in the same working directory and if you try running your program from the current working directory than, your program must get executed without any problems as, java tries to find your .class file is your current directory. If your class files are present in some other directory other than that of the java files we must set the CLASSPATH pointing to the directory that contain your compiled class files.CLASSPATH can be set as follows on a Windows machine:

How to set the CLASSPATH variable

Steps for set the CLASSPATH

1. Click on Start button > right click on ‘my computer’ > properties

2. Click advanced > environment variable

Classpath is set when we get ClassNOTFound Exception. To solve this error we set the classpath

“C:\Program Files\Java\jre1.6.0\lib\rt.jar” here rt.jar is a jar file where all the .class files are present so we set the classpath upto rt.jar.

NOTE: Finally after set classpath Restart your system, or you can reopen command
prompt.