When we compile or run java application using cmd prompt without setting path variable, It will not compile but gives error below shown in cmd screen.
This error is mainly due to cmd is not recognizing the javac command or java tool and other java librararies, hence to know these command to cmd we need to give the path of these tool (javac). So This path is nothing but the location of javac tool.
And path is---C:\Program Files\Java\jdk1.7.0\bin i.e we are giving path of jdk installed on our machine.(****You should have java install)
• How to set setpath variable to run java application through cmd prompt?
There are two ways to set setpath variable ,as below
1. Temporarily setting path through cmd only
2. Permanently setting path through (system)environment variable
Firstly, let’s see first Approach
In this approach we set path variable using cmd like setpath=C:\Program Files\Java\jdk1.7.0\bin
and is valid to current open cmd instance only. If we exit cmd and open new cmd instance then again we need to set path variable hence we called it as temporary path variable..
let’s see screen of cmd to set path variable
Now we can compile and run java program
Main drawback of temporarily setting path is each time we open new instance of cmd we need to set path again and again.
To overcome this we have Second Approach.
In this we set path variable using environmental variable like process given below.
1. Right click on computer------go to the properties and click on it
2. Go to the Advance System Setting and click on it
3.Now Click on Environmental Variables..
4.Click on New...in User Variables
5. Write variable nameas ‘path’ and variable value should be the path of jdk like
And finally goes clicking on OK button
Set classpath as well 2nd screen
Now open cmd ,You can compile and run java program without writing set path variable on cmd prompt like below.