In this tutorial we will look at how to set path and classpath for Java in Windows-7, I assumed that the Java SDK and Java runtime environment is already installed on your system.
Setting the path for JDK and JRE is important for Java programs to compile and run. If the path and the classpath are not set, or not set properly, compiling java program will show errors, saying javac is not recognized as an internal or external command, this is because of the reason that path for javac or java is not set.
To set the path you can select my computer, right-click it, select properties, click on the advanced system settings on the top-left corner of the window, this pops up systems property, with the Advanced tab selected, click environment variables button, in the environment variables button, you have two options of setting user variables or system variables.
It is better to set paths in the system’s variable, you may or may not have path variable inside the system’s variable tab if you already have the path variable, then you can click edit by selecting the path variable or you need to create a new variable by clicking the new button, and now click the edit button and the system variable path will be under the variable name and next you need to provide the physical path to the folder where JDK and JRE are installed.
The video below demonstrates how to set path and classpath for Java in Windows-7.
Setting Path and ClassPath for Java in Windows 7
You might already have the path inside the variable values, do not delete this path, but try to append this path, you can type the complete path, but it’s better to browse to the location and copy the path, I have installed Java under C Drive in Program Files, we need to browse till the JDK bin folder and select the entire path from the address bar, copy and paste it, at the end of the existing path by appending with a semicolon.
For JRE we need to again append the path with a semicolon, then browse till the JRE bin folder, select the entire path from the address bar and paste it in the variable values, now click OK and you will find the path appended.
Now we need to set classpath you may or may not have classpath variable inside the system’s variable, if you already have the classpath variable then you can edit or else you need to create a new variable by clicking new button, in my case I do not have the classpath variable, I’ll click a new button and create a classpath, next we need to provide a physical path to the folder of JDK and JRE, I have installed Java and C Drive and the Program Files for JDK we need to browse to the JDK folder and bin folder copy the entire path from the address bar and paste it, appended by a semicolon, now for Class, we need to browse till the JRE bin folder copy the entire path from the address bar and paste it in the variable value, appended by a semicolon and the dot at the end, and click ok and close the system properties, close all the command terminal in case they are open.
Now open a new terminal window and try to compile Java program, the java program will be compiled, as well as we can execute the Java class file, in case you are getting the same error as javac is not recognized as an internal or external command then you need to recheck your Java path as well as the class path.