HelloWorld : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

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.

Version With Packages

Beginners start out writing simple programs that do not have a package like this:

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

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

javac.exe HelloWorld.java

and to execute it:

rem execute
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 helloworld.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.

    rem do NOT do this
    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. If your HelloWorld is an Applet rather than an application, you need some HTML (Hypertext Markup Language) 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
  6. Did you remember to make your class public?
  7. 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 )
       {
       }
  8. 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.

Version With Packages

As soon as your projects need more than one class to solve, you will want to use packages. In this example, we will presume you own website mysite.com. To make your package globally unique, you choose to name the package for your HelloWorld as com.mysite.gettingstarted. HelloWorld.java needs to live in a directory called X:\com\mysite\gettingstarted\. mindprod.com code typically lives in packages with names like com.mindprod.xxxx. HelloWorld.class also needs to live in a directory called X:\com\mysite\gettingstarted\. The current directory needs to be X:\ for java.exe to be able to find the class. The name of the class you put on the java command line would be com.mysite.gettingstated.HelloWorld. The full truth is a little more complex, e.g. you can have zip and JAR files too.

// HelloWorld with a package
package com.mysite.gettingstarted;
public class HelloWorld
   {
   public static void main ( String [] args )
      {
      out.println( "Hello World" );
      } // end main
   } // end class HelloWorld

Set up the classpath:

rem set up classpath. Better done permanently in Control Panel.
set classpath=X:\;.

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

X:
cd \com\mysite\gettingstarted
javac.exe HelloWorld.java

and to execute it

rem execute HelloWorld
X:
CD \
java.exe  com.mysite.gettingstarted.HelloWorld
rem -30-

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 helloworld.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 . X:\com\mysite\gettingstarted\HelloWorld

    is not permitted.

  4. Make sure the current directory is the parent of the com package tree
  5. 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
  6. Did you remember to make your class public?
  7. 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 )
       {
       }
  8. 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.

This page is posted
on the web at:

http://mindprod.com/jgloss/helloworld.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\helloworld.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[54.204.104.71]
You are visitor number