Introduction to Java Development Kit (JDK)

In order for us to develop Java applications, we need to first install the Java Development Kit (JDK). The JDK contains the necessary tools to compile, execute, debug, document, etc. our programs.

The installation process is pretty straightforward. We just need to download the latest version of the JDK from Oracle’s website. The Java edition we’re going to use here is Java SE.

You can download the JDK by itself or with one of the bundles available. In this post, we’ll use the JDK only, but you could go ahead and download the bundle that includes NetBeans since we’ll use it in later posts.

Select the appropriate installer for you operative system. In my case, I’ve downloaded the JDK for Windows 64.

After you have downloaded the JDK installer, double-click on it to start the installation.

JDK installer screenshot

There’s nothing special about the installer. Just click Next on every screen like in any other program =)

Once you have installed the JDK, you’ll find it in your Program Files folder, unless you chose a different path during the installation. In my case, it’s located at C:\Program Files\Java\jdk1.6.0_23, where 1.6.0 is the version number and 23 is the update number. Inside this folder, there’s the bin folder, which contains all the JDK tools, such as the Java compiler and Java class loader.

Let’s verify we can call the JDK from any location. Open a command prompt (Start -> All Programs -> Accessories -> Command Prompt), type “javac” and press Enter.

javac is not recognized error

If you get the error “’javac’ is not recognized as an internal or external command, operable program or batch file,” you’ll need to set up the environment variables for Windows. Otherwise, you can skip the next section.

Setting Up Environment Variables

Right-click on “My Computer” and go to Properties. Go to the Advanced tab. You should get to a window similar to this one.

Windows system properties

Click on the Environment Variables button. In the window that pops up, find the Path variable under the System variables section. Select it and then click on the Edit button just below it.

Windows environment variables

In the next window, place the cursor in the Variable value text box. Press the End key to go to the end of the line. Do no delete anything; just need to append the following. Type “;” followed by the full path to the JDK. In my case it would be “;C:\Program Files\Java\jdk1.6.0_23\bin”. Now click OK on the three open windows.

Windows Path variable

Close the previous Command Prompt if you still have it up and open a new one. Type “javac” and press Enter. Instead of the error you received before, you should get all the options available for the javac command.

Javac command line options

Now you can call the JDK from any location.

Writing Your First Program

Open your favorite text editor and type the following code. Please note that Java is case-sensitive, so you need to remember exactly how you name your class.

// This class will contain the entry point of the program
class MyFirstJavaApp
{
	// This is the entry point of the program
	public static void main(String args[])
	{
		// Write something
		System.out.println("Hello World!");
	}
}

Save the file somewhere in your computer as MyFirstJavaApp.java. I saved mine to C:\Blog\Code\Java\MyFirstApp\.

Breaking Down The Code

We start by defining a class. Java is an object-oriented language, and classes are the core of object-oriented programming. All code in Java needs to be contained in functions, and functions have to be inside a class.

Next we define the entry point of the program. The compiler expects a function named main and should be public and static.

Lastly, we just write a string to the console. We call the println function and pass is our message.

Compile And Run The Program

Let’s now compile our source code to Java bytecode so we can execute it. Open a command prompt (Start –> All Programs–> Accessories –> Command Prompt) and navigate to the folder where you saved your code.

Change folders in command prompt

Call the Java compiler (javac.exe) and pass the file name. Please note that this step is not case-sensitive because we’re passing the file name, and Windows is not case-sensitive when it comes to file names. Linux is a different story. Also note that the .java extension needs to be used.

Javac command prompt call

If everything was correct, you should not get any message. A file with the same name but with the extension .class should be created in the same directory. If there was a problem with the code, you’ll get an explanation of the error.

Now let’s execute our code. We need to call the Java Class Loader (java.exe) and feed it with our class name. Please note that this step is case-sensitive. You need to use the same case that you used when you defined the class in the code. Also note there’s no extension at the end.

Java command prompt call

There you have it, you printed a message to the screen using Java! Leave a comment if you ran into any issue or if you have any question.

Get Free Updates
Related Posts
Comments