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.
Table Of Contents
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
If you see this on your computer, then you have to set up your Computer to run JAVA code.
Also check out:
- How to hide a drive in windows to protect our data..?
- How to make bootable pendrive or Disc for OS installation.?
- Big Iron, Extremely powerful Computer. Why do hackers stay away from it…?!!
- How does Hard Disk work..? What is inside it..??!!
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 Runtime 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?
- Right click on My Computer and then click on Properties.
2.Then click on Advanced System Settings
3.Then click on Environment Variables present in Advance tab.
4.There are two sections under Environment Variables. User variables and System Variables. The 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 the system variable. No problem actually.
5.Click on new to add a new variable.
6.At the Variable name, box writes 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 represents 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)
7.Add another variable named path.
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 represents 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.
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.
Run a Java Program
After these above steps, you are ready to write your first java program. See the steps below to create java file, compile and then run that….
Create and save .java file
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. The filename should be same as the class name on that code. For example ClassName.java
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 example 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.
Check out the Hot topics:
Compile a java source code
The above file named ClassName.java is called source code. We need to do few more steps to get the output from these lines of codes. 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‘ the file will be generated in this current directory (i.e D:\Applications). Here className.class file will be generated.
Run java program
After compilation, we can run that program.To run your Program enter a Command “java ClassName” in your CMD. It will show you the output of your program.
ALL THE BEST.. and HAPPY LEARNING..:)