Thursday, September 17, 2009

Hello World ... is anybody out there?

Now that you have a project open in Eclipse, it's time to get down to the business of writing and running a program. We're going to start with the ever popular "Hello World" program.

Java programs are built from text files with the extension ".java". The .java files are organized in directories called "packages". So, in order to say "hello" to the world, we will need to create a .java file in our project.

To create a .java file, right-mouse click on the project folder in the navigation window and select "new->class". This brings up a dialog window with a bunch of options. Most of the defaults should be OK. You will want to make sure that the "Source folder" is set to [your project name]/src and you will need to give it a name. The name of the class file must be the same as your class (capitalization counts). So, we should pick a good name.

As a convention, I like to use nouns for the names of classes, — I find this helps maintain the whole object-oriented paradigm
— so, let's call the program "Greetings". Also by convention, we start class names with a capital letter.

Once you've entered the name of the class, click on the "Finish" button. (You might see a warning at this point stating the the use of the default package is discouraged. Just ignore that for now, we'll talk more about that later.)

You should now see a new file in the src directory called Greetings.java, like this:


The file itself will just have this:



Now you are ready to build your class that will be your Hello World program. Put this code in between the curly braces of your new "Greetings" class:



public static void main (String[] args){

String strGreeting = "Hello World!";
System.out.println (strGreeting);

}
Save your Greetings.java file, and you are ready to run it. (Eclipse will automatically pre-compile your .java file into a .class file ready to run.)

To run a Java file in Eclipse, locate the "run icon" in your navigation bar and click on it.



If you've done everything correctly, you should see something like this in the output console (generally found in the bottom tile of the Eclipse screen).




Tadaa, and congratulations! Welcome to the World!

Wednesday, September 9, 2009

Where do I start?

Welcome to Java Elements, Tips for Beginners. My name is Ray and I will be your host as we explore some of the fundamentals of Java and Java programming. This blog is intended to give me a way to supplement a beginning Java programming class that I teach. Hopefully, it will also prove handy to others who would like a little help getting started with the Java programming language.

There are plenty of sites with information on how Java operates and the theory behind it. One of the best sources of this kind of information is, strangely enough, the SUN website. Really!

I know, I know. Who would have thought that the same people who created Java would also tell you how it works and why it is so cool? Granted, they did all that in a rather staid fashion, but then, SUN is a company (was a company?) that grew up in an era when programming was done on mainframes and took a lot of "geek" power (and math) to do.

Here's the URL to get you started on the SUN site, learning about what Java is: http://java.sun.com/docs/books/tutorial/getStarted/intro/index.html.

With that preamble out of the way, let me get you doing something rather than just reading about doing something.

Like all good artisans (and programmers ARE artisans), you need the right tools (applications) to work your chosen medium (Java).

You can use unwieldy, archaic, build-it-yourself tools like note-pad and the Java development tools, if you really want to. Or, you can use a state-of-the-art Integrated Development Environment (IDE) like Eclipse. Either way, the apps are free.

Let me suggest you start with Eclipse. It's really not that difficult. Just go to the Eclipse website and download the "Eclipse IDE for Java Developers." Be sure to get the latest, production version. (I am currently running Eclipse Ganymede.)

Download and install Eclipse. There, that wasn't so hard, was it?

Once Eclipse is installed, fire it up. Double-click on it's icon or start it from the "start" menu. You know enough to get to this blog, you should know enough to start up an application.

When it starts, it may prompt you for a project directory. Unless you are a real control freak, just take whatever default it gives you.

When the project directory dialog goes away, you may see the Eclipse "Welcome" page. Click on the curved arrow to "go to the Workbench."

Your workbench will usually start out with three panes visible to you (there are more panes that can be viewed and more ways of viewing the ones you have, but we'll talk about that more later). The three main panes are:
  • Navigation (on the left)
  • Workspace (on the top right, taking up most of the real estate)
  • Messages (on the bottom right)
Before you can start writing a program in Eclipse, you will need to create a "project." Projects group your program files together and provide links to the code libraries you will be using. Assuming your installation went smoothly, however, you should be able to build a project using standard defaults relatively easily.

To create a project:
  • Go to File->New and select "Java project"
  • Give your project a name, any name will do
  • The defaults should be OK (Create a new project in Workspace, Use default JRE, Create separate folders for sources and class files)
  • Click Finish
You should see a new project appear in the navigation pane. Click on the arrow next to the project to see the files in your project. You should see a folder labeled "src." This is where we are going to put all your program files.

UP NEXT: Creating a Hello World program in Java!