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!

No comments:

Post a Comment