Episode 2: Preparing the environement
Hello dear friend, it looks like this is a perfect day to start programming. In this episode you will prepare your machine for using Java and write your first Java program. The installation and configuration will be the least fun part of this episode and probably of all the upcoming ones, but don’t worry, if you follow the steps carefully it will be as easy as pie, and you’ll be able to finally enjoy coding :).
Installing the JDK
JDK stands for Java Development Kit. This is a software that you need to install on your machine if you want to write code in Java. The task of this software is reading your code, checking if it’s correctly written, and if so generate a file that can be executed by the Java Virtual Machine (or JVM) , which is a software included in the JDK.
First, let’s check if you don’t already have the JDK installed, in which case you may jump to the next section. Go to Start and click on Run… as shown below.
This will open a command line window in which you will type the following command followed by enter :
javac -version
In the above example you can see that the result of the command was javac 1.6.0_03 meaning that I have the JDK already installed on my computer, and the installed version is 1.6.0_03.
If the JDK is not installed on your machine then you should have, instead of the version, a message saying:
‘javac’ is not recognized as an internal or external command,
operable program or batch file.
In that case all you have to do is downloading and installing the JDK:
Go to the Java SE Downloads Page , locate the JDK download button like shown below and click on it :
You will be redirected to another page where you have to accept the License Agreement. Then locate the suitable JDK for your system and click on it to download it. In my case I am using Windows XP so I downloaded the “Windows x86” JDK as shown on the following screen shot.
Now that you downloaded the JDK (jdk-7u10-windows-i586.exe in my case) simply double click on it to launch the installation and keep clicking on the Next button until you see the close button (which have to be clicked on too )
Now the JDK is installed on your machine on C:\Program Files\Java\jdk1.7.0_10 (or depending on the version you installed) you just still have little configuration to do.
Right click on My Computer, and click on Properties.
On the advanced tab, Click on the Environment Variables as shown below.
In the system variables click on New to create a new environment variable.
This environment variables indicates the path to the JDK directory. Ensure that its name is JAVA_HOME and its value is C:\Program Files\Java\jdk1.7.0_10 (or depending on the location of your JDK) and click on OK.
Now that the JAVA_HOME variable is created double click on the existing Path variable in order to modify it. Then add %JAVA_HOME%\bin; at the beginning of the variable value as shown below (don’t modify anything else 😉 ):
click on OK and you’re done :). But to check that everything went as expected open a new command line window (Start -> run -> cmd) and execute the two following commands (each one followed by enter of course 😉 ):
- javac -version
- java -version
If you don’t get a message saying that javac (resp. java) is not recognized then congratulations, you have correctly installed and configured the JDK
Installing Eclipse
Actually you can start coding in Java right now using your JDK and any text editor like the old Bloc-notes :). But writing Java code using such a text editor is as efficient as painting your house using a nail brush. Instead of that, we are going to use an integrated development environment (or IDE) which is a software that makes coding very easy. Even better, among all the available Java IDEs, we are going to use the number one: Eclipse.
To download Eclipse go to the official website and choose Eclipse IDE for Java EE Developpers as shown on the screenshot below:
You will be redirected to another page where you will be able to download your Eclipse package. Click on the download icon as shown below and save the eclipse-jee-juno-SR1-win32.zip file somewhere on your computer.
The zip file contains a single directory named eclipse. Extract this directory from the zip file and move it to a location with a meaningful name. For example create a directory in D: with name development and move the eclipse directory to it so that the path to your eclipse directory is : D:\development\eclipse
In the eclipse directory you will find a file named eclipse.exe, if you only see a file named eclipse that would mean that file extensions are hidden. Here is how to show them :
Open any directory, or double click on My Computer, and click on Tools, then on Folder Options… as shown below.
On the View tab uncheck the option Hide extensions for known file types and click on OK as shown below.
Right click on eclipse.exe, click on Send To and the on Desktop (create shortcut) as shown below.
You have just created a shortcut on your Desktop, that will be used to launch eclipse. Go to your Desktop and right click on that shortcut and then on Properties.
In the Target filed add a space follewed by the following string :
-vmargs -Xmx768m -XX:MaxPermSize=384m
Click on Apply and then OK.
Eclipse is now ready to launch (oh yeah !), double click on the eclipse shortcut and let’s get down to business
Playing with eclipse
When you launch eclipse for the first time it will ask for the path of your workspace. The workspace is simply a directory in which all your work on Eclipse is stored. For example type D:\Development\workspace in the Workspace field. Mark this as your default workspace by checking the check box and click on OK as shown below.
You are now in the Java EE Perspective. Let’s switch to the Java perspective. Click on the button Open Perspective shown below.
Among the proposed perspectives Double click on Java
Now let’s create our very first Java project on Eclipse: a simple Hello World program.
Go to File -> New -> Java Project
As project name type in HelloWorld and click on Finish.
Now your project HelloWorld apears on the package explorer on the left of the workbench. Right click on it, then choose New and click on Class as shown below.
Let’s name this class Main and click on finish.
The newly created class Main appears as a file named Main.java as shown below.
Double click on Main.java to open it and let’s start coding. You will find that Eclipse has already written some code for you :
155 156 | public class Main{ } |
We are going to modify this code just a little bit: our new code will look like the following :
1 2 3 4 5 6 7 | public class Main { public static void main(String[] argv) { System.out.println("Hello world"); } } |
The goal of the above code is very simple: we want the computer to print the string Hello world on the screen. Let’s execute it to see if it works ;). Click on the Run arrow then choose Run As and click on Java Application as in the screen shot below.
At the bottom of the workbench you will be able to see the Console tab in which you have the result of the execution of your program: Hello world is displayed as expected
The slightly grayed button on the above screen shot means that the execution of the program is completed. It can be used to terminate a program by force when you judge it suitable
Congratulations !
And there you have it ! You have been able to prepare your development environment by installing the JDK and Eclipse, and writing your first Java program. It is true that you don’t totally understand what does each of those lines mean but don’t worry, everything will come on a timely manner.
Now that the environment is ready, the fun will start in the next episode where we will talk about variables and operators which are part of the basics of the Java language.
Goodbye and see you in the next episode.