Thursday, July 5, 2012

Working With Java JDK

Note: Before I begin this tutorial of how to test your Java JDK, I need to make sure you know that any questions you have can be asked below as blogger.com allows users (you) and authors (me) to communicate with each other on the bottom of the page.
----------------------------------------------------------------------------
This tutorial assumes you have the Java JDK downloaded properly. If not, you can follow this tutorial.
OK, so you have the Java JDK installed but now we need to test it out. We'll start out my creating a java file that prints out 'Hello World'. Open notepad and copy in the following code:


public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}


Save it somewhere temporary such as your desktop:
FileName: HelloWorld.java
Save as type: All Files(*)



Now we have to execute the java file. Open up command prompt by opening cmd.exe . You can just search for 'cmd.exe' on your start menu search bar:
Once command prompt is open, you should see this window:
We will be using this to run out HelloWorld.java file, which will work if the Java JDK is installed and ready. Next is to change the directory to where we saved the HelloWorld.java file, which was our desktop. Type The following the command prompt window, where UserName represents your windows username:
cd C:\Users\UserName\Desktop
Click Enter and you should now be in your desktop directory. Now we will compile the HelloWorld.java file so we can run it. Enter in the following command:


javac HelloWorld.java


If you see no error after hitting enter, then your Java JDK is installed and configured correctly. If you the folloring error, then we still have some work to do. I received the following error after typing 'javac HelloWorld.exe':



c:\Users\Yousef\Desktop>javac HelloWorld.java
'javac' is not recognized as an internal or external command,
operable program or batch file.



No problem. This is because we haven't told our computer where the Java JDK is. Now let's fix this problem.


We'll start by finding out where we installed the java jdk bin folder is. The default location is:


C:\Program Files\Java\jdk1.7.0_05\bin
Of course, you may have a different version of the Java JDK.


Copy the location of the directory.

Now go to Start>Right Click on Computer> click Properties:
Go to Advanced system settings:


Then click on Environment Variables:


On System variables, go down to path and click edit:


At the end of Variable value, add to the text the location of the bin Java JDK folder:


Click OK for all windows we just opened.
Now we try to compile our HelloWorld.java file again. Re-open 'cmd.exe' and type the collowing command, where UserName represents your windows username:
cd C:\Users\UserName\Desktop


Click enter and enter the follow command:
javac HelloWorld.java


Click enter. If you see no words come up, then your java JDK is configured correctly (even though we're not done running the HelloWorld.java program). Now we can execute the program. Type the following command:


java HelloWorld


If all goes well, you should see the text "Hello World!" display on the command prompt:
Congratualtions. Your Java JDK is set up correctly. Remember, any questions can be left below as a comment. Happy programming!

How To Download And Install Java JDK

Before I begin this tutorial of how to download the Java JDK, I need to make sure you know that any questions you have can be asked below as blogger.com allows users (you) and authors (me) to communicate with each other on the bottom of the page.

First step is to visit the Java SE Downloads page. Once there, you will be asked what exactly you want to download.
Your options are:
  1. Java Platform (JDK)
  2. JavaFX 2.1.1
  3. JDK 7u5 + NetBeans
  4. JDK 7u3 + Java EE
Now these may be out of date as the lastest one for me right now is 7u. Yours may be 8 or above but essentially you don't have to worry about this.

What we want to download is the first one being Java Platform (JDK). Click on the first box which will take you to the Java SE Development Kit 7 Downloads page. Scroll down to Java SE Development Kit 7u5, or the first gray-squared box:
The second gray box labeled Java SE Development Kit 7u5 Demos and Samples Downloads is exactly what it says, Demos and Samples, so we don't need that for the installation of Java JDK.


Now we Accept License Agreement and choose our version to download:



Let the JDK finish downloading then open it. Now we configure the instillation. During the installation process, just click next for default installation settings. You should be seeing this window after clicking 'next' for each step:
Once the instillation has finished, click 'continue':

You now have the Java JDK installed onto your computer.

You then have the option of installing 'Java FX' which takes up ~ 50mb of space. It is not required to program with java, however, it is useful when working when graphical user interfaces and other scripting languages. I went ahead and installed it because I have space, and may one day use it.
Java FX:
Your next step is to Test Your Java JDK!