Journey into SuperWaba

Ever wonder what it takes to write programs for your PDA? I did, and so now I am beginning a journey to try my hand at writing something for the PalmOS. SuperWaba, a variation of Java for the PDA, is my language of choice. And I'm a novice, which makes it all the more unpredictable. Hope you'll come along for the ride! Note: You can read multiple posts on one page if you click on an archive link.

Saturday, July 10, 2004

Java HelloWorld

Getting Started with SuperWaba – Java HelloWorld

Now I’m pretty sure that this entry is going to be frustrating for some. Why you ask? Because maybe you are chomping at the bit to write something on your Palm PDA, or read about me doing that, and here I am telling you that we’re going to take a step away from PDAs and SuperWaba to focus on Java! Yes, we probably could jump right into a Palm app on the handheld without too much difficulty. But the whole thing is still pretty intimidating to me so you’ll just have to bear with me as I do this step by step.

My goal this time is simply to (1) get some familiarity with Java and (2) write and compile a Java HelloWorld program on the desktop (WinXP).

I figured that if I couldn’t do something simple in Java, I would be totally lost when it came to trying out something in SuperWaba on the Palm. That is probably a realistic assessment. For some of you, if you’re new to all this and are following along doing your own thing with me, this step may take a bit of time. And writing a HelloWorld program may not be enough to feel a base level of comfort with Java. So you might want to go a bit farther with Java than I plan to do right now. At the same time I hear SuperWaba calling my name, so I do want to get to that soon.

Even though I call myself a novice, I do have a little non-GUI Java programming under my belt from the past work, and have both taken classes and done some work with OO design. That means I don't know much, but even to get going I needed this quick refresher now about the most basic of basics. And hopefully, that will jog my memory about some of the other basic level stuff I used to know. Since it won't take much to get me lost, I’ll dive into my Java Beginners book often.

I originally learned some Java from being put on the hot seat at work, plus a little from a book called Beginning Java (JDK1.1) by Ivor Horton. I like that book because it is simple and it doesn’t spend a lot of time on goofy stuff that doesn’t seem useful for basic desktop application programming. I also am pretty sure there’s an updated book now for Java2.

Now that’s something else I should explain in simple terms. Java versions went something like 1.0, 1.1, 1.2, 1.3, 1.4, and currently as of this writing I think 1.5 is in beta. Things changed pretty significantly going from 1.0 and 1.1 forward into 1.2 and onward. As a result, people seem to refer to 1.2, 1.3, 1.4 etc collectively as Java2. Go figure! The bottom line is that while you probably want to consider a book that uses 1.4, you especially want to make sure it’s at least Java Second Edition, or Java2 or whatever it’s called. I.e. Make sure it covers 1.2 or higher.

And if you’re looking for a good Java book, you probably don’t want to pick a book that talks about Java frameworks like Struts, or distributed objects and stuff like RMI or CORBA. (I understand that’s already like a legacy technology now anyway!?) You don’t need all that enterprise stuff now. You just need to know about the basic Java language, some simple thoughts on OO design, and how to deal with conditional logic, variables, classes and objects, inheritance, interfaces, methods, file manipulation, threads, AWT/Swing, etc. Here’s where I could probably point you better if I knew where we were headed (i.e. if I already knew Java and/or SuperWaba). But you’re along for the ride with a novice, so you’ll just have to take what I can offer.

After a lot of reading at the bookstore, the book I decided on was perfect for me as a refresher. And looks to be easy to read, covers topics I’m interested in, and is really down to earth and practical. It’s Murach’s Beginning Java2 by Andrea Steelman. (And I just noticed the similarity in titles… both books I liked are called "Beginning Java" so I must like that name for Java books!) I also have my eye on Java in a Nutshell by O’Reilly Press. But I didn’t get one because I have a Java 1.1 version, which may hold me until I find out more about SuperWaba and what I might need.

Okay, let’s get down to the nitty gritty and get that HelloWorld application running. The first thing you need to decide is what editor or IDE to use. (As I noted before, and IDE is just an integrated development environment to make it easier to code.) I decided to do HelloWorld in three places just for the heck of it. In notepad, TextPad, and JBuilder. (More registrations and downloads!!!!! I sure hope you have a broadband connection if you’re doing this with me. I can’t imagine doing all this downloading without one.)

But for our purposes here, we’ll just do it in notepad to stay generic. It will be more than enough to get the point across.

There are several things you need to do to get ready to edit and compile the code. And all of this will be in any book you have chosen on beginning Java.
1) Pick and create a directory on your PC to store your code in. I recommend something like C:\MyJavaCode because it’s simple and easy to access even from a command prompt. Plus, you may want to have directories under that for each project/application.
2) Set the path variable in Windows to include the java compiler. It’s called javac.exe and it’s one of the files that gets installed with the Java SDK. This path statement tells Windows where to look for files to execute, so when you call the java compiler, it knows where to find it. You will need to add to the path variable the name of the directory with javac.exe in it.
3) Set the classpath variable in Windows. This is another environment variable for Windows, kind of like Path, but this one is especially for Java. The idea is that Java code is grouped into Packages that contain classes. The classes are text files that define the program and contain the code, including it’s variables and program instructions. Java is different than a language like Cobol in that it isn’t all lumped into one big file. It’s made up of a bunch of classes, and each class is defined in a separate text file.

The packages and subpackages are set up like, and correspond to, directories and subdirectories. In fact, all the code files (classes) that make up a program are grouped into directories with names equal to their package names. The classpath statement tells Java where all the “top-level” package directories are. For what we’re doing, we might not ever have to create our own packages, so you can think of the classpath as pointing to the directory with your code.

Because we’re going to use a command line compiler, and call it from the same location as the code files, all you need to add to the classpath variable is “.” To represent the current directory.

If all this is confusing to you, get yourself that beginning Java book. It’s really not that hard, and those books can explain it a whole lot better than me.

Now that we’ve installed the Java SDK, and set the path and classpath variables, we are finally ready to write and execute a HelloWorld program.

Here’s the code I’m going to use. It will go in the text file C:\MyJavaCode\ which has to match in name to the class name defined in the code we put inside that file.

public class HelloWorld{
public static void main( String[] args){
System.out.println(“Hello to the World from the SuperWaba blog!”);

Note that this blog may not show the indents properly. Each of the first three lines gets a different level of indent. The first "}" should be under the start of "public static...", and the second "}" is back at the left margin. Maybe later I'll figure out better how to control the formatting on this blog, but I'm in a hurry to finish this post right now.

To compile this code (from a command line on WinXP and from the same directory as the file with the program in it), you just type >javac (the ">" is the command line prompt, so don't type it in) and hopefully you get no errors, or at least they are easy to fix.

To run the program, you then type >java HelloWorld and the message should appear on the console.

That’s all there is to it, and that’s all for this time. Next time we look at SuperWaba and make a stab at HelloWorld on my SJ33!


Post a Comment

<< Home