Monday, 18 August 2008

Hello World!

Because if you didn't know, getting your program to print the sentence "Hello World!" is the standard for the first program you write in any language, why? I don't know.

Ok, first up, notepad. That's right, good old notepad. Open up notepad because were going to write the first program in it, compile it using the command line interface and then run it. After that were going to do the same program in the Netbeans IDE. Why? Because everyone should know how to write, compile and run a Java program without an IDE. After you can do that, then consider choosing an IDE and learning to use that. Ill explain the benefits later.

Here are some things you may need to know about the whole process as a whole...

You write your code in text documents, which are saved as .java files. This is your source code. If using an IDE it may append other files and folders to better organise your work, but for a simple program, remember the source code is saved in .java files. .java files can be compiled into .class files. The compilation process is such that the javac compiler turns your source code .java files into machine readable Byte Code (.class files). These can be understood by your computer via the Java Virtual Machine (Remember JVM?) which means it can be run, theoretically on any machine with the JVM interpreter. So once your source code is compiled into byte code, you can run your program. Hope that's clear...

I'm going to give you the code, all you have to do is copy and paste it. At the moment don't worry about the details, in the next post I will explain it. Right now I think its important to be able to get through the compilation process and just get the program running before getting into specific details.

/* My first Hello World program */

class HelloWorld{

      public static void main(String[] args)
      {

            System.out.println("Hello World!"); //Print Hello World in the console
      }
}

Paste that into notepad and save it as "HelloWorld.java". It must be called HelloWorld.java, and nothing else. This is because the main class name must be the same as the file name. Save it onto your desktop.

Now open up the command prompt. Start Menu, and either in run or in the search box type "cmd". Now you need to locate your HelloWorld file. My console starts in C:\Users\Aaron. To get to the desktop type "cd Desktop". Use the "dir" command to display the list of files and folders in your current folder to find where your going. If you cant get to your desktop, just save the HelloWorld.java file in a folder you can find.

If you type "dir" now, it should have a list containing HelloWorld.java. To compile this, type "javac HelloWorld.java". This uses the javac compiler to compile that java file. A common mistake here to use "javac HelloWorld" this is wrong, you must state the full file name. Another problem people run into when compiling their java files is that they get "javac is not recognisable" or something like that. This means that the path is not setup within the console environment for it to find the javac compiler exe file within your computer. Go here http://www.phoons.com/john/classes/aboutpath.html to learn how to do that. If your still having problems drop me an email at aaron (at) gravityapple.com and I will get back to you usually within a day. (yeah I check my emails like every hour...)

If you get any errors, it is probably because you didn't copy and paste the code right, the file isn't called HelloWorld.java, your not in the correct folder in the console or your javac path is not set up properly. Go here http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html if your having problems compiling and follow the Sun Microsystems steps. If you did'nt get any errors, your ready to run! Notice if you type "dir" and check the file list, there will be a new file called HelloWorld.class. This is the bytecode class file I was talking about which is the actual file the Java Machine runs. Type "java HelloWorld" (no .java this time). The console should print "Hello World!". Congratulations if it does, because you have now successfully created a java file, compiled it and have run it successfully. Give yourself a pat on the back, and an email telling me how much you love it.

Next post will be on how to do the same thing, (Hello world!), in Netbeans. I'll also go through what an IDE is and why you should use one.



0 comments: