Java J2SE Hello World sample program

Contributor Icon Contributed by Rex Date Icon September 4, 2004  
Tag Icon Tagged: Java programming

The Hello World program (one that just prints Hello World to the screen) has long been a staple first application for a programmer to try out a new programming language. This recipe describes the steps involved in compiling and running a simple Hello World program in Java.


You must first download a Java JDK on your system (check first, there may already be one.. look for an executable called javac or javac.exe). Look for the highest version number that does not end in RC (for release candidate). Make sure to download the JDK (Java Development Kit), not the JRE (Java Runtime Environment). The develepment kit includes the java compiler. Install the JDK as described in the release notes for your platform.

In your favorite editor, create a file called HelloWorld.java with the following contents:

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

From a command line, the command to compile this program is:

javac HelloWorld.java

For this to work, the javac must be in your shell’s path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program.

To run the program, you just run it with the java command:

java HelloWorld

It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).

Previous recipe | Next recipe |
 
  • Anonymous
    :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted:
  • efren
    GOOD
  • efren
    NICE WORK
  • elow do know how to create a program in java
    please reply to me i will wait
    thank you
  • prasad
    hi my name prasad i am run simple program in that program only compile
    after compile the program run the program shows the message Exception in thread "main" java.lang.NoClassDefFoundError this will appear what is the
    problem plz send me reply
  • Edukondala Venkata Reddy
    Executing your program using this command should correct the problem:
    java -classpath . helloworld
  • ashish anand
    thank u very much Mr Reddy..

    ashish
  • honey
    yes i am coputer science student
  • Johnny
    what if I don't have javac.... When installed.... javac is missing in java 1.6.0
  • please create a new simple program not only a hello world
    thank you
  • yung maganda naman po!!!
    nakakasawa kasi lagi nalang hello wolrd alam
    na kaya namin yun lahat yung iba naman para kakaiba dibah!!!
  • Edukondala Venkata Reddy
    nice language... Seems to be java7 version.
  • Edukondala Venkata Reddy
    Useful blog.
  • mn25
    Nice, thank you guy
  • honey
    hi poh i'm honey do u know how to program about average??
blog comments powered by Disqus