Java J2SE Hello World sample program

Contributor Icon Contributed by Rex  
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).

 

24 Comments -


  1. efren said on November 26, 2008

    GOOD

  2. efren said on November 26, 2008

    NICE WORK

  3. Johnny said on November 27, 2008

    what if I don’t have javac…. When installed…. javac is missing in java 1.6.0

  4. marjorie pagalan said on January 24, 2009

    please create a new simple program not only a hello world
    thank you

  5. marjorie pagalan said on January 24, 2009

    yung maganda naman po!!!
    nakakasawa kasi lagi nalang hello wolrd alam
    na kaya namin yun lahat yung iba naman para kakaiba dibah!!!

  6. marjorie pagalan said on January 24, 2009

    elow do know how to create a program in java
    please reply to me i will wait
    thank you

  7. prasad said on January 29, 2009

    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

  8. Edukondala Venkata Reddy said on March 10, 2009

    Executing your program using this command should correct the problem:
    java -classpath . helloworld

  9. Edukondala Venkata Reddy said on March 10, 2009

    nice language… Seems to be java7 version.

  10. Edukondala Venkata Reddy said on March 10, 2009

    Useful blog.

  11. ashish anand said on June 30, 2009

    thank u very much Mr Reddy..

    ashish

  12. Anonymous said on July 14, 2009

    Nice, thank you guy

  13. honey said on July 15, 2009

    hi poh i’m honey do u know how to program about average??

  14. honey said on July 15, 2009

    yes i am coputer science student

  15. Anonymous said on February 16, 2010

    no answer fot this program,iam using jdk1.3

  16. Anonymous said on February 16, 2010

    good think

  17. kush om sharma said on June 16, 2010

    aap ney jo “hello ka program” dea usko bhi execute kraya ye to execute ho gya but mera bnaya program execute nhi ho rha hai same problem exception in thread “main”…..etc please tell me how remoove this problem ????

  18. Skyzthelimit said on June 28, 2010

    trying to create and run a random access file getting a error of exception in thread “main” java.lang

  19. Karunasanapala said on August 26, 2010

    but no use

  20. Prasanna said on April 12, 2011

    Good example

  21. B Sravankumar1990 said on April 13, 2011

    first write program on notepad and save it as .java
    javac filename.java for compiling and
    java filename for running
    or call to 7569891072

  22. B Sravankumar1990 said on April 13, 2011

    give the class name as the filename.first letter of filename and classname must capital letter

  23. Vincentcastro_93 said on July 9, 2011

    What does this eror mean?

    Exception in thread “main” java.util.InputMismatchException    at java.util.Scanner.throwFor(Scanner.java:840)    at java.util.Scanner.next(Scanner.java:1461)    at java.util.Scanner.nextInt(Scanner.java:2091)    at java.util.Scanner.nextInt(Scanner.java:2050)    at Case.bubbleSort(Case.java:40)    at Case.main(Case.java:24)

  24. govindarao said on October 12, 2011

    plz send me core java simple programs

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -