Posts
60
Comments
36
Trackbacks
18
Monday, May 25, 2009
Java: Getting Started

There is one question that I get asked pretty normally, so I am going to address it here.  When new to Java, the one thing that everyone asks, whether it is to me or to themselves or someone else is... How do I get started with a program?  There is only one real way to answer this.  I use a tactic called "Hello World" or "Hey" or whatever  favorite term comes to mind.  This means that I start a web project by producing those words programmatically through Java in html.  Or when I am creating new AJAX functionality, I often send those words first, before I tie in any data or logic.

So, in this post, we are going to go back and see how we might create a basic command prompt Java application.  I will discuss in short why we do what we do as well.  Before you run this example, you need to make sure Java is installed properly.

1. Start -> Run -> type notepad, press enter.

5-25-2009 2-20-43 PM

2. The first class in the file must be public and also named exactly as the file is going to be named.  These are nuances that are absolutely imperative to understand, though the reasoning behind them aren't as much.  So we will start by typing:

public class HelloWorld
{

}

3. Click File -> Save, then choose a location.  To follow my examples for the command line, you should create a Java folder on your hard drive (i.e. C:\Java).  Once you have browsed to your location, name the file HelloWorld.java.  Click Save.

5-25-2009 2-30-20 PM

4. Open a command prompt.

5-25-2009 1-49-35 PM

5. Change directory to the location of your HelloWorld.java file.  I will type cd C:\Java.  I will then attempt to run my program by typing javac HelloWorld.java.  If everything goes well, you can browse to the location (C:\Java) and witness two files in your folder.  One is HelloWorld.java and HelloWorld.class, the javac command, produced your class file. (Note: My first error was because I failed to save the file in the previous step #3)

5-25-2009 2-37-23 PM

6. Now we need to add the required static main class so that the Java Compiler knows what to do when we request to run our file.  This is required because the compiler will try to invoke HelloWorld.main(args) programmatically to kick off our process (Note: args is the array of strings that you can pass in on the command prompt, more on that some other time just know that it is a required parameter of the main method).  I modify the file in notepad to include this method as follows.  After you have this text in notepad, you can save the file and run step 5 again to make sure all of your syntax is proper.  If you have errors you will have to resolve these to continue.  Notice the placement of the method inside of the braces for the class HelloWorld.  This is the basic organization, if you mess up your brackets it is very likely your program will not operate properly, so this is important.

public class HelloWorld
{

  public static void main(String[] args)
  {

  }

}

7. There is a method, of System.out that we can invoke to print a friendly message to our command prompt.  This is how we modify to achieve our first Java program and a good way to start off other programs in the future.

public class HelloWorld
{

  public static void main(String[] args)
  {
    // Do stuff here
    System.out.println("Hello World");
  }

}

8. Once again, carry out task 5 to make sure your program compiles.  After you have compiled your most recent version of this file, you can type java HelloWorld and you should see it respond by printing out Hello World to you below your call.

5-25-2009 2-50-08 PM

In this tutorial you learned how to start a Java program.  This is the way that I always start off, to make sure all of my initial wires are plugged in.  Of those of you that may find difficulty in this tutorial there are a few habits we need you to get used to as soon as possible.  Pay attention to case, that is where your capital letters are.  In Java and programming there are many ways to do it, but we generally use Camel Case.  This allows everyone to be on the same page and is a very good habit to get use to that.

Please leave comments with help for others, where you may have run into issues and such so that everyone can benefit here.

posted @ Monday, May 25, 2009 2:55 PM | Feedback (0)
Using Command Prompt with Java

There are several things that I like about using the command prompt with Java.  When beginning to program it can be much less cumbersome than using a integrated development environment (IDE) like Netbeans or Eclipse; where the errors are less descriptive as I will demonstrate here.

To start off, I need to download a Java Development Kit (JDK), and I usually like to use the latest.  In this case it would be Java 6 JDK (at the time of this writing it is update 13, i.e. JDK 6 Update 13).  You can also download the documentation from there as well.  To view the documentation  on the web, you can look here at Java docs.

After selecting my operating system and acknowledging my agreement to their terms, I downloaded and installed JDK 6.  For now I will not register my product, so, with Java installed, I am ready to set up my environment so I can use Java (This example is in Vista 32bit, x86).

1. Click your start button, and roll over Computer and right click.  Select Properties.

5-25-2009 10-41-22 AM

2. Click Advanced Settings

5-25-2009 10-47-10 AM 

3. Select Environment Variables

5-25-2009 10-51-49 AM

4. Find your Path Variable under System Variables, Click Edit.

5-25-2009 1-34-01 PM

5. At the end of the Variable Value, make sure there is a semi colon.  This is where we are going to put the path location of the bin folder of our Java SDK.

5-25-2009 1-37-31 PM

6. Browse to your install location for Java (default is: C:\Program Files\Java\jdk1.6.0_13\bin), making sure you are in the bin folder, click the down arrow in the browser bar.  This will produce the Path variable you need to append in the previous note. Copy the location and go back to your Edit System Variables window(described in 5, above).  (Note: XP Users will need to do the same thing in their ClassPath variable as well)

5-25-2009 1-39-29 PM

7. Make sure you put a semi-colon after your path (to minimize future errors).

5-25-2009 1-45-08 PM

8. Click Ok in Edit System Variables, Ok in Environment Variables, and Ok in System Properties.

9. To test my class path, I go to a command line prompt (Start->Run-> and type cmd, Press Enter).

5-25-2009 1-49-35 PM

10. From your command prompt, type javac test, and press enter.

11. An error like the one below means that it is installed properly.

5-25-2009 1-51-08 PM

 

In this series you learned how to install the Java SDK, Set your Path variable and we also learned how to run a quick test to see if Java has installed properly.  Please leave tips/comments/errors for others to see so that we can make this post a little more comprehensive.  Hope you enjoyed reading.

posted @ Monday, May 25, 2009 1:54 PM | Feedback (3)
News
My Developer Notebook! This also happens to be my opinion place. Thanks for coming by.