Thursday, February 17, 2011

Java : Hello World

We're dragging our feet a bit here as "Hello World" programs are mind-numbingly dull. But, we've got to start somewhere. So, with a rolled howl, we'll lead our low lol herd to a lewd ho roll (in ASCII). ("Hello World" doesn't even have any good anagrams.)

First off, create a desktop shortcut to make it easy to start a DOS box in your Java working directory. On our Windows XP laptop, we copied the "Command Prompt" shortcut from <start><All Programs><Accessories> to the desktop. Right click on the DOS prompt icon and you should see "Target: %SystemRoot%\system32\cmd.exe". Change the "Start in:" field to your working Java directory. In our case "C:\Documents and Settings\Sucio\Desktop\Java".

On Windows 7, the Target would be "%windir%\system32\cmd.exe" and we'd probably start in "%HOMEDRIVE%%HOMEPATH%\Java" which comes out as "C:\Users\Sucio\Java".
(I changed the name of my shortcut to "JAVA Prompt.)

So, I'm sure you're itching to try something so let's get started. Here's the text of our simple "Hello World" program.

class HelloWorldApp {
    public static void main(String[] args) {
      System.out.println("Viva Sucio!");
There's a very good description of what every word of this program is doing at <Jimmy Wales' blog>. For now, we'll just say that the program defines a class of objects called HelloWorldApp and tells Java what the object can do (its methods - in this case, just the method main).

Copy and paste the simple program into a new file "" in your working directory. (Note, a java program must have a file name ending with ".java"; and the name must match that of the class defined by the program - in our case "HelloWorldApp".)

Once you've saved the program, at the command prompt, type


and press enter. If you haven't screwed up, there'll be a slight pause and then you'll get a new prompt as though nothing's happened. But, if you list the directory contents, you'll find that there's now a new file called "HelloWorldApp.class". (If the PC complains that it can't find javac, it's because you didn't set the PATH environment variable properly. Follow the link in yesterday's post to find instructions.)

So what happened here? The Java compiler ("javac") compiled (translated) the Java class definition in our program source file into bytecode. Bytecode is a special kind of machine instruction which can be run (interpreted) by something called the Java Virtual Machine (JVM). the bytecode program is stored in a ".class" file.

Rather than waffling let's have a go: type

java HelloWorldApp

and press enter. Your "Hello World" message (in our case "Viva Sucio!") should appear on the screen, followed by a new prompt.

The JVM ("java") loaded the bytecode in "HelloWorldApp.class" and started running the program at the "main" entry point. The "main" method told the JVM to print the line of text "Viva Sucio!" to the system output stream.

Hopefully some the the jargon in our last post is getting clearer now. We downloaded the Java Runtime Environment (JRE) which contained the Java Virtual Machine (JVM - "java"). We also downloaded the Java Development Kit (JDK) which contained the Java compiler ("javac").

Play around a bit changing the message string in the program.

    ,/,         ,.%((.)))             |.
   '///_,      (///)\\)))          ,  |||/
    \  ,'     (([]-[]-\\\)         y-";,-"
     | |       )| -    @)))        | |
     |  \      ((\___,"((         /  |
     |   `---.__))|   |())___.---'   |
      `-._       "-_   _ -"       _,-"
          "-.__,"         ".__.-"
              ( @   / \   @ )
               `---'   `---'
                |         |
                |    .    |
               /           \
              /    "-.-"    \
             /      /^\      \
            /      /   \      \
           /      /     \      \
          (    ."        ".     \
           \    )          \     \
            \   ".          `.    \
             ".   \            \   \
               `.  `)           \  ".
                 \  (            \  \
                .mno/            |   \
"Sarah Palin nude" - That should get the google hits up.

(Based on an image by hjw at the ASCII pôrn archive.)


  1. I would avoid a nude Sarah Palin like the plague.

  2. Yikes, Sarah Palin nude..... hello world!

  3. Sarah Palin seems like a nice lady on the news:D

  4. I can't tell whether her arms are droopy/flabby or muscular. Either way... I need to go throw up now.

  5. Y'all hating... Palin is a MILF. She might be batshit crazy, but a MILF nonetheless.

  6. @ Rorschach Redemption - "Main.Java"? Should be called "". The filename has to match the public class. ("main" is a method of class HelloWorldApp.)

    Confusing and frustrating I know.

  7. I actually managed to delete my Java final....You know those buttons on the side of some mice that go front a page/back a page?.....You should be real careful not to click those when you've already gone 2 hours over the time limit on a final.

  8. Ooooooooooooooooooooh.
    I thought I could get away with changing names to protect...nothing.

    Public class? NO SUCH THING!


  9. Yeah and those are what her b**bs look like? sure Sarah...

  10. @ Rorschach Redemption - it's pretty lame that the compiler doesn't give a more meaningful message for an error that 99.9% of noobs will make (myself included).

    Reminds me of my first Pascal compilation in Unix. I got the error message "bad magic number". It took 3 or 4 hours of reading man pages to work out that I'd left the ".p" off the end of the program file name.

  11. @ Marlene - I think she's had space implants.

  12. Even though that's suppose to be a Sarah Palin, it's kind of neat looking.

  13. Why is Sarah Palin holding up her middle fingers? I find that obscene.

  14. something something something something Sarah Palin nude. haha

  15. Ah, needed some more palin porn.
    Also, I should get into java. I hear it's good for games devs to know.

  16. You know, that art perfectly portrayed her substance.