Java Glossary : HelloWorld

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : H words : HelloWorld.

HelloWorld
HelloWorld is usually the first Java program a novice writes. It is a trivial program that just displays the string "Hello World". This is not very impressive, but getting it to work proves you have your classpath, compiler and runtime all correctly installed and that you understand the basics of compling and running. This is a surprisingly difficult task. So break out the champagne when your little HelloWorld works.

public class HelloWorld
   {
   public static void main ( String [] args )
      {
      System.out.println( "Hello World" );
      } // end main
   } // end class HelloWorld

From the directory where HelloWorld.java lives, to compile it, type:

javac.exe -classpath . HelloWorld.java

and later run it with:

java.exe -classpath . HelloWorld

If this does not work, here are some likely problem areas:

  1. The name of the source file must be HelloWorld.java, precisely, and the name of the public class in your source must be HelloWorld, precisely, including case. If you use notepad, you may have inadvertently named your file hellowworld.txt or helloworld.java.txt.
  2. You specify the .java extension to compile, but leave the .class extension off to execute. How logical!
  3. You may not specify the fully qualified drive and path: e.g.

    java.exe -classpath . C:\TEMP\HelloWorld
    

    is not permitted.

  4. As a corollary, make sure the current directory is where the HelloWorld.class file is.
  5. You have to get the case exactly correct. e.g.
  6. java.exe -classpath . Helloworld
    

    is not permitted.

  7. If your HelloWorld is an Applet rather than an application, you need some HTML commands to invoke it. You don't just load it in your browser as if it were a web page.
    Applet for more details on composing that HTML
  8. Did you remember to make your class public?
  9. Did you remember to make your main method public and static? It must have a signature exactly like this:

    public static void main ( String [] args )
    

  10. If you used the abbreviation javac instead of the fully qualified name such as javac.exe, check that the javac.exe you wanted is on the classpath and is the only javac.* on the classpath.
If you have a more complex program, using a package, the name of the class you put on the java command line would be com.mindprod.mypackage.MyClass. MyClass.class needs to live in a directory called C:\com\mindprod\mypackage. The current directory needs to be C:\ for Java to be able to find the class. The full truth is a little more complex, e.g. you can have zip and JAR files too.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 3031.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/helloworld.html J:\mindprod\jgloss\helloworld.html