Quantcast
Viewing latest article 11
Browse Latest Browse All 15

1.2 Setting up the Java Environment

This post is part 2 in the “Introduction to Java” tutorial series. If you missed the first part, have a look at an Introduction to Java.

In this post, we are going to get your computer ready for Java. Setting the Java Run-time Environment (JRE) on your computer is very easy. First, you have to download the Java Development Kit (JDK) from the official Java website, which is here (for now, you can download only the JDK, without NetBeans). You can see that are a lot of downloads available.  You need to download the Java SE JDK binary for your platform, which is both the OS and the processor architecture. For example, my computer runs on a 64 bit processor and the Windows operating system. Hence, I download the binary or the installer for this configuration.

Once you download the installer, you need to install the JDK on your computer. The installation procedure will vary depending on the type of installer you downloader. In this tutorial series, we will be working on a Windows machine, and hence, all the instructions will be provided for the same.

Now, assuming that you have downloaded and installed the JDK on your computer, it is time to check if the JDK is properly installed or not. To do this, open command prompt. You can do this in two ways. One, go to the Windows Start menu and look for Command Prompt under Accessories, and two, use the Windows Run tool. Press and hold and Windows key on your keyboard and hit the “R” key on your keyboard. This will bring up the “Run” window. Type “cmd” in the “Open:” box and hit Enter. The Command Prompt will appear.

Here, type “java” at the prompt and hit Enter. You should now get the java help information. If you get this and not an error, your JKD is installed properly.

Image may be NSFW.
Clik here to view.
Check java in CMD

Similarly, type “javac” and hit Enter. Now, you need to get an error as shown in the screenshot below. Now, worry not, this is expected. You are getting this error because the system does not know where your Java Compiler (javac) is located in your computer.

Image may be NSFW.
Clik here to view.
Javac Error in CMD

To resolve this error, you need to set the “environment variables” in your computer. To do this, right click on the “My Computer” or “Computer” icon on your desktop, or the Stat menu, or the Windows Explorer. Now, if you are on Windows XP, go to the “Advanced” tab, and if you are on Windows 7/8, click the “Advanced system settings” link on the left pane in the “System” window that appears. You should arrive at the “System Properties” window, as shown below.

Image may be NSFW.
Clik here to view.
System Properties

Here, you have the “Environment Variables…” button at the bottom. Click the button to get the “Environment Variables” window. This window has two sections, the “User variables” and “System variables”. We are going to make some changes in the System variables part.

Image may be NSFW.
Clik here to view.
Environment Variables

Now, open My Computer and navigate to the Java installation folder, which usually will be something like “C:\Program Files\Java\jdk1.7.0_25”. Here, you will find a “bin” folder. Go inside the folder and then copy the path to that folder from the address bar of the window.

Now, come back to the “System Properties” window and search for the “Path” variable in the “System variable” section. Select “Path” and click the “Edit…” button. In the “Edit System Variable” window, click the “Variable value:” box and hit the “End” button on your keyboard. You will now be at the end of the string. Now, place a semi-colon (;) at the end of the string and press Ctrl+V on your keyboard to paste the path to the JDK folder you had copied. Now, the Edit System Variable window should look something like this.

Image may be NSFW.
Clik here to view.
Edit System Variable

Click the “Ok” button. Now, in the “System variables” section, click the “New…” button to set up a new system variable. In the “New System Variable” window, type “JAVA_HOME” in the Variable name box and paste the same path in the Variable value path. Now, remove the “\bin” at the end of the value string. So now, the variable value should be something like this.

Image may be NSFW.
Clik here to view.
JAVA_HOME varable add

Click the “Ok” button. We need to add just one more variable. So click the “New…” button again. In the variable name box, enter “JRE_HOME” and paste the same path in the variable value box. But now, add “\jre” to the end of the value string. Your setup should look something like this.

Image may be NSFW.
Clik here to view.
JRE_HOME varable add

Now, click the “Ok” button to close the window, and click the “Ok” button in the other windows as well. Also, close the Command Prompt if you have it open, and then open it again. Type “javac” again and hit Enter. Now you should get an output something like this.

Image may be NSFW.
Clik here to view.
Check javac in CMD

If you get such a response, you have successfully created the required environment variables. Now, your computer knows where Java is and where all the required Java commands are. So you can now compile any of your Java code from anywhere on your computer. But if you get an error again, you need to repeat the process, as you might have missed out something.

In the next part, we will write and execute our first Java program, which is the good old “Hello, World!” program.


Viewing latest article 11
Browse Latest Browse All 15

Trending Articles