Tutorial

Java Hello World Program

Published on August 4, 2022
Java Hello World Program

Whenever we start to learn a programming language, the first program is always to print the Hello World. In the last article, we learned how to install Java on Windows 10. Now we are ready to write and run our first Hello World Java program.

Java Hello World Program

To keep things simple and working for a new user, here is the sample hello world program that you can use.

public class JavaHelloWorldProgram {

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

Save above program as JavaHelloWorldProgram.java in any directory.

1. Compile and Run Java Hello World Program

Open Command Prompt and go to the directory where the hello world program file is saved. Then execute the below commands in order.

$javac JavaHelloWorldProgram.java

$java JavaHelloWorldProgram
Hello World

java hello world program

If you are using Java 11 or higher, then you can simply execute java JavaHelloWorldProgram.java and it will compile and execute the program for you. No need to explicitly compile and then run the java program.

2. Java Program important points

  1. A Java source file can have multiple classes but only one public class is allowed.
  2. The java source file name should be same as the public class name. That’s why the file name of our program is JavaHelloWorldProgram.java
  3. When we compile the code, it generates byte code and save it as Class_Name.class extension. If you look at the directory where we compiled the java file, you will notice a new file created JavaHelloWorldProgram.class
  4. When we execute the class file, we don’t need to provide complete file name. We need to use only the public class name.
  5. When we run the program using java command, it loads the class into JVM and looks for the main method in the class and runs it. The main function syntax should be same as specified in the program, else it won’t run and throw exception as Exception in thread "main" java.lang.NoSuchMethodError: main.

I have recently created a short video for Java Hello World Program using Notepad and then Eclipse. Watch it for a better understanding. https://www.youtube.com/watch?v=ZREpFyjTDho That’s all for this post and you can start playing with your first class. In the next post, I will get into further details of classes, JDK, JVM, and other features provided by the Java programming language. Update: Read this post to know about JDK vs JRE vs JVM in java.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Pankaj Kumar
Pankaj Kumar
See author profile
Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
December 17, 2015

Excelent

- Leandro

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 31, 2016

    Excellent… But i have a question… If a public class is present in any java source file…why we should save the file name with the public class name??

    - PRAVEEN KUMAR BADAM

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 24, 2016

    We can save filename with different name. hence there is no rules to save Filename always with ClassName. At the time of compilation you just give your filename. After compilation you will get .class file with your class name.[classname.class]. But at the time of loading your program into JVM(Java Virtual Machine) you just have to give the class name.

    - Aliasger

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 17, 2017

    My public class name is Test & saved class file as Test1.java. Here you go: C:\>javac Test1.java Test1.java:1: error: class Test is public, should be declared in a file named Test.java public class Test{ ^ 1 error Your case is only valid when your class is not public class & it allows you to do compilation only because of its fail when you execute the same class. C:\>javac Test1.java C:\>java Test.java Error: Could not find or load main class Test.java

    - HITESH SINGH

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 21, 2017

    If you have a public class It should always be saved with the public class name

    - Aliasger

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 24, 2018

      if your class name is test then you have to save class file as test.java here you have to follow C:\>javac test.java C:\>java test

      - Waseem R Khan

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        July 21, 2016

        excellent

        - Sarawgi K

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 26, 2017

          i’m satisfied with this first topic

          - Prakash Bale

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 24, 2019

            Thank you for amazing tutorials! Is it a good idea to add “how to create .exe / DMG / pkg” for this Hello World tutorial?

            - Nik

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              March 5, 2019

              public class ThankYou{ public static void main(String [] args){ System.out.println(“Thank you very much a good start form a beginner”); } }

              - Adam

                Join the Tech Talk
                Success! Thank you! Please check your email for further details.

                Please complete your information!

                Become a contributor for community

                Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                DigitalOcean Documentation

                Full documentation for every DigitalOcean product.

                Resources for startups and SMBs

                The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                Get our newsletter

                Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                New accounts only. By submitting your email you agree to our Privacy Policy

                The developer cloud

                Scale up as you grow — whether you're running one virtual machine or ten thousand.

                Get started for free

                Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                *This promotional offer applies to new accounts only.