Java – The Hello World Example

June 6, 2009 by: Allen Sanford

Like any good book on programming my first article about programming in Java starts with the Hello World Example. It has become common place that if you are going to write a set of programming tutorials in a given language that you start with “Hello World!”, so I will not deviate here. “Hello World” examples stand to serve one purpose, to expose the beginning programmer to both the language syntax and programming environment (This includes the editor of choice, and the compiler if applicable) without a lot of complicated logic to confuse the newbie.

Now those of you who know me by now know that almost always my programming tutorials are geared towards web development and this on is no different. We will be creating a small Java applet, this type of Java program can be embedded in a Web page and can be very useful for a lot of useful things. OK, now let’s create the applet, embed it in a web page, and run the applet now.

Creating the Hello World example

The rest of this article makes the assumption that you have the Java Development Kit (JDK) installed on your machine. If you do not then you need to go here => JDK Download Site download the JDK, verify that it is working and then continue with this tutorial. You will also need a text editor so you can modify, create, and save .java files. I don’t like it when people try and tell me which editor works best for me so I am going to extend the favor and not make any suggestions here, I just recommend that you understand how your particular editor handles saving files with different extensions. Just make a note here that the Sun Java Compiler will not understand anything other than straight text files so be careful when saving those files with Word or WordPad.

OK, OK, enough let’s get to it.

?Download hello.java
1
2
3
4
5
6
7
8
    import java.awt.Graphics;
    public class hello extends java.applet.Applet
    {
        public void paint(Graphics g)
        {
            g.drawString("Hello World!", 50, 25);
        }
    }

Just worth making a note here Java is case sensitive the file name case and the class construct must match including case. Save this files and let’s move on to compiling it.

Compiling the Hello World Applet

This is simple, simple, simple ready, at the command line make sure you are in the directory the source file is saved at, and type the following at the command line.

1
    c:/> javac hello.java

After this has completed there will be two files in the current directory now hello.java and hello.class. Now you are ready to use your “Hello World” applet. Now I am not going to get all technical and tell you what just happened just know that your applet is ready to go.

Running the Hello World Applet

To see what your applet looks like while running we will need to embed it into so HTML. So I will keep this brief and simple. In the directory where you hello.class and your hello,.java files are create a new file named hello.html. Inside this file place the following lines. Of coarse my example is valid XHTML but you could use and doctype really.

?Download hello.html
1
2
3
4
5
6
7
8
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>Java Hello World Example!</title></head>
    <body>
        <applet code="hello.class" width="100" height="100"></applet>
    </body>
    </html>

Now to see your newly created applet you have two options, one use the JDK included applet viewer or you could open the hello.html file, or two open hello.html with any java enabled web browser such as Firefox or IE. I prefer the second so I can see it in a web browser, after all aren’t we developing this applet to use on the web?

The Conclusion

I am a learn by example kinda guy so I am not going to give the technical break down of every line of Java code and what exactly it does, it is my experience that most people can figure it out on there own. If you are own of those who can’t figure out what just happened on each line then I am assuming that you have never had a class on any programming language and would assume that you need to take one before you read any more of my tutorials, or just go with the flow and you’ll figure it out.

Filed under: "Left Overs"
Tags: , , ,

Comments

One Response to “Java – The Hello World Example”
  1. kleanchap says:

    I am a newbie to Java programming. Thank you for the good start! My question is are all Java applets (the compiled code) used in browsers only? I want to process lot of log data in real time and show it in the browser. How can I achieve that?

    Thank you once again for this simple/easy start!

Leave a Reply