How to easily set up a Computer to run First Java code

People who start leaning java_programming and try to run a piece of code on their computer generally face many problems. Even though their Java code is correct they can’t run it on their Computer. There are many things you have to set up to run a program in java on your System. The details of those required things we will discuss here.

If your Computer is not set to run a Java program and you try to compile any program with javac command then we will get error message like this

javac is not recognized error

If you are seeing this on your computer then you have to set up your Computer to run JAVA code.




  • Download And Install JDK on your Computer

JDK stands for Java Development kit and it’s a software which acts as a development environment. It contains Executable files like Javac(Java Compiler) &  java(Program loader/interpreter), All the inbuilt Libraries for Java Program, JRE(Java Run time Environment) and other tools to develop a java application. If JDK is not installed in your computer then Download it from here.

Then Install JDK on your System. By default it will get installed on “C:\Program Files\Java\jdk-xx” this directory.

After installation of JDK also our java code won’t run. The same “javac not recognized” Error will show. You have to set Environment Variables.

  • What is Environment Variable ?

Environment Variables are Global Variables those are defined to access by all the processes run under an Operating System. It stores many variables like USERPROFILE, USERDOMAIN, APPDATA, TEMP, CLASSPATH, path etc. Here the path variable contains the directories of executable programs. In case of java we have to run javac and java (Two executable programs of JDK) in order to compile and load our java program. So we have to set the directories of these two files in path variable so that when we enter these javac and java command in CMD, it will able to find it and execute it. When we run javac or java command on command prompt then process search for these executable files by the directories we have set in path variables.

  • How to set path Variable?
  1. Right click on My Computer and then click on Properties.

Computer Propoerties

 

2.Then click on Advance System Settings

3.Then click on Environment Variables present in Advance tab.

Environment variable step-1

 

 

 

Environment variable step-2

4.There are two sections under Environment Variables. User variables and System Variables. Value set under User Variable can be accessed by this user only and the Value set under System Variables can be accessed by any user of your computer. So you can add path variable to either User variable or in system variable. No problem actually.

5.Click on new to add new variable.

6.At Variable name box write JAVA_HOME and in Variable value box write the directory where your JDK is installed. Generally it is C:\Program Files\Java\jdk XXX;     Here  XXX represent the version of jdk you have installed. Here I have Installed jdk1.8.0_101  

//(Followed by a semicolon represent that directory is over here)

JAVA_HOME environment variable

7.Add another variable named path.

Variable name=path

Variable Value= .;C:\Program Files\Javajdk1.8.0_101\bin;   (Where your JAVAC and JAVA files are present).

java path variable

            Note: Here the value of path Variable is  .;C:\Program Files\Java\jdk1.8.0_101\bin;      Before our directory we have added ‘.;‘  . …. ‘.(dot)’ always means current directory. We added .; that represent a colon after a dot. There might be any other path values internally added. By adding .; before our directory we’re just confirming  that we are not mixing our value with others values present before. That way we are protecting other path values from getting corrupted. 

8.If your path variable is already present then edit that and in value box then after the semicolon just add this value C:\Program Files\Java\jdk1.8.0_101\bin;





Your environment variables are set now. To check open your command prompt (Press Windows key + R, Write CMD and press Enter key), then Type javac and press enter key. If your result will show some help options command then your setting is okay.

javac command

If your cmd screen still shows ‘javac’ not recognized Error message then your setting is not done correctly and you have to check the process all again.

After these steps you are ready to write your first java program. Create one new Text Document and write your java code in that. Then Save that file as Save as type “All Files” and the filename with an extension .java . File name should be same as the class name on that code. For example ClassName.java

save a java code

This .java file is saved at D:\APPLICATIONS

Then open CMD and compile this code. You should point your CMD directory to the folder where your .java file is located. For exmple here my file ClassNmae.java is present in a folder named “APPLICATIONS” in “D” Drive then your command prompt should direct to “D:\APPLICATIONS”. For this type ‘D:‘ without quotation mark in CMD and press Enter. And then type ‘cd Applications‘ and press enter key. Now your CMD should be pointing to D:Applications.

Then enter a command “javac ClassName.java” and press enter key to compile this program. If your code is  error free then it will compile successfully and a ‘.class‘ file will be generated on this current directory (i.e D:\Applications). Here className.class file will be generated. Then to Run your Program enter a Command         “java ClassName” in your CMD. It will show you the output of your program.

run java code

 

Hope this article will be helpful for you.

ALL THE BEST.. and HAPPY LEARNING..:)

Add Comment