≡ Menu

Java Hello World

Your first Java Program – The Java Hello World Program

So, what does Java Hello World even mean anyway? Well, in the programming world, the term “Hello World” typically refers to the most basic program that can be written and run in a given language. Think of it like this, if you can write a Java Hello World program, this means that you were able to:

  1. Setup the necessary tools
  2. Write the code
  3. Correctly compile the code
  4. Run your program

 
This isn’t always the easiest task to accomplish when you are new to a programming language. So, as it pertains to Java, if you can accomplish all of these tasks and successfully write your Java Hello World program, then you are off to a good start. Now, we have already covered step 1 above, in a previous post. So let’s move on to step 2, writing the code.

First, Launch your Spring STS tool, you’ll notice that you get stuck on this screen:
Java Hello World - Pic 1
 
You’ll just need to pic a folder that will house your development files. I usually choose C:\DEV\, but it’s your choice. So, pick a folder and click OK.

Now, with Spring STS open, you’ll need to start a new project, so choose File -> New -> Java Project. You’ll be presented with this screen:
Java Hello World
 
Be sure to fill out a program name, I used “helloWorld”, but you can use any name you like for this project. Next, you should have the JRE installed (we covered this in the last post), but if for some reason it didn’t install correctly, just leave a comment and I’ll see what I can do to help you get setup! Now Click Finish.

You may or may not be presented with this window, if you are just click the “Remember my decision” and click Yes:
Java Hello World
 
Now, if all went well, you should see something like the following:
Java Hello World
 
We need to create a new file for this project, so click File -> New -> Class. So now you should see this screen:

Be sure to fill out two fields: Package and Name, for the Package I used “com.helloworld”, for the Name I used “HelloWorld”. You can see this in the screenshot above. Now click Finish.

That should have created a new Class file called “HelloWorld.java” and you should now see something like this on your screen:

Now we are ready to write some code! If you like, you can copy/paste the following code into your HelloWorld.java file:

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

Now, for the final step, you’ll need to run this! If you right click your mouse anywhere in the code area, you’ll get a menu that will have a menu item named “Run As“, hover over it and select “Java Application“. This will run your code and output “Hello World” into your console. If you don’t see the console, just click Window -> Show View -> Console, like so:


 

Voila, you have just setup, coded, and run your first Java program! Welcome to your first accomplishment as a Java developer. If you have any troubles following these steps or need any assistance, please leave a comment below and I’ll try to help you out as best as I can.




Don't code another minute until you have these tools.
Get this 1-page PDF that outlines the top 7 Tools that Java Professionals use every day.

{ 44 comments… add one }

  • Hasan August 11, 2012, 3:12 am

    Hey, love your tutorial so far. I was just wondering what the ‘package’ is and if there are any specific naming requirements – ie do you have to name it ‘com.enterThingHere’ or can it have the same name as the class?

    • Trevor Page August 11, 2012, 3:17 am

      Hi Hasan,

      I forgot that I needed to do a post about what packages are in Java, thanks for reminding me. Give me about 12-18 hours (as it’s currently 4am here) and I’ll write up a tutorial and post it. Feel free to sign up for my mailing list here so you know when it’s live.

      Take care,

      Trevor

  • Kwan August 14, 2012, 3:00 pm

    why does the next time i open the file i cannot ‘run’ the code anymore? when i rightclick the menu that appears isnt the same! help plz!!!!

    • Trevor Page August 14, 2012, 3:09 pm

      Usually to run the code, you need to right click on the “main” method and choose “run as” -> “java application”. Are you right clicking the “main” method?

  • Josh August 15, 2012, 2:42 am

    Think this tutorial is wonderful. I love being exposed to a different language. I’m really starting to like Java, in large part due to this tutorial.

    One suggestion I have (and maybe I just couldn’t find it) is if you could either have a link to get to the next “Unit” or list them in a more chronological order, so I could more easily know which lesson is next. Just a suggestion. Keep up the great work!

    • Trevor Page August 15, 2012, 8:59 am

      Hey Josh,

      Thanks for you interest in my tutorials, and you make a great point. I started to add the links for my first five posts, not sure why I stopped. So I’ll add those in ASAP.
      Take care,

      Trevor

  • Busayo August 16, 2012, 5:29 pm

    Yep! Finally found my way back on track. Josh made a great point, so people me don’t get lost more than once. And you finally got me to ACTUALLY write my first Hello world program. Been seeing it in books but never got this far to even start doing it! Big ups!

    • Trevor Page August 16, 2012, 5:44 pm

      I had already put in those “Next Java Tutorial” links at the bottom of each post. Was there a particular spot that those links were missing? Perhaps I messed that up

  • visoth pich August 22, 2012, 7:54 pm

    hi i was wondering how to connect to two different classes together into one program like say i have this:

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

    AND i have this class:

    package com.hmm;
    
    public class Hmm
    {
         public static void main(String[] args)
               { 
                    System.out.println("Hmm.");
                 }
    }
    

    i know these two programs are the same and you can jsut put the “Hmm.” into the HelloWorld program to make it work, but im jsut using it as an example for my question, how would i get these two into one prgoram?

    • Trevor Page August 22, 2012, 8:34 pm

      Good question!

      To tell you the truth, you would likely never have two classes that would each have a “main” method in one project (one program). The whole point of the “main” method is just the starting point (or entry point) to an application. Where the code flows from there will depend on which new Objects you instantiate and your use of control structures. So the better example of how one Class would “connect” to another would be like this:

      package com.helloworld;
      
      public class HelloWorld
      {
        public static void main(String[] args)
        {
          System.out.println("Hello World");
          Hmm newHmmClass = new Hmm();
        }
      }
      
      //--------------------------------//
      
      package com.hmm;
       
      public class Hmm
      {
        // this is a constructor, and this code will be executed when
        // the Hmm class is instantiated
        public Hmm ()
        {
          System.out.println("Hmm.");
        }
      }
      
      • jonna dela cruz July 16, 2013, 6:58 am

        can i ask something about java program. how to creat java program using eclipse

        • Trevor Page July 18, 2013, 3:11 pm

          The tutorials here show you how to create Java programs using Spring STS IDE (which is just a fancier version of Eclipse). So if you follow along with these tutorials, you’ll have your answer.

  • Salamat Ali October 1, 2012, 4:56 am

    Java is a Product of Sun Micro system. so java has no his own IDE. What is the best 3rd party IDE of java?

    • Trevor Page October 1, 2012, 7:18 am

      That’s definitely a subjective question, but i use the SpringSource Tool Suite in my tutorials. I find it works best for me

  • seph October 10, 2012, 2:17 am

    I found this material very useful, informative and educative. Thanks for a job welldone, however I’ll need more streamlined help as I had already installed NetBeans 7.2 on my computer before I came accross this awesome tutorial.

  • Christian October 17, 2012, 1:39 pm

    So far this has been a big help and I am glad i found this.

  • Sagar neupane November 6, 2012, 2:02 am

    Hi trevor,
    Thank you very much. Man this is the one I have been looking for. I have been through each and every words of your site. As a beginner, i have been looking through so many sites of programming language but no of them had done well enough to make me understand, but your one is superb mate. This is really the best site I have visited so far. The way you have described and the way you have given examples is really appreciateable and fantastic. I am really impressed by your work. For the beginners likes us, It could have been more better if you had provided your site with some important and basic java programs so that we could practice and make the concepts clear. I mean create the questions(write a program to…..) and give the solution as well. God bless you.:-)

    • Trevor Page November 6, 2012, 10:05 am

      I always appreciate positive feedback, but I like it even more when my readers make suggestions on ways I can improve the site!

      What you’re suggesting is kind of like a homework assignment yes? I’d ask you to write a program that should so “something”, let you attempt to follow through with this, and also provide a solution so you can peek if need be, yes?

  • joe December 19, 2012, 11:07 am

    I get what sagar is trying to say. Maybe incorporate some “practice problems” somewhere? That would really help people like us who are trying to just get into Java. Like give us a goal and we have to accomplish it?

    right now im newer to your site so I wouldn’t know if there are any up yet, but I intend to stick around:)

    • Trevor Page December 19, 2012, 1:47 pm

      Joe,

      I don’t have any practice problems up yet, but I’m literally working on one right now. If you haven’t already, just sign up for the mailing list, and you’ll get an email when it’s live.

      Take care,

      Trevor

    • Trevor Page December 29, 2012, 4:47 pm

      Hi Joe,

      Just wanted to let you know that I’ve published the first assignment (just in case you didn’t see it go up). Here’s the URL: https://howtoprogramwithjava.com/java-practice-assignment-1/

  • Katie February 4, 2013, 2:12 am

    Are there any good tools to download for Macs? I dowloaded the JDK SE 7, but it wouldn’t open on my Mac.

  • Hunter March 2, 2013, 12:37 pm

    every time i run my hello world (i have a mac and it has eclipse indigo on it) a pop up appears that says
    “The selection cannot be launched, and there are no recent launches.” please help

    • Trevor Page March 2, 2013, 3:13 pm

      Are you right clicking on the “main” method and selecting “Run As->Java Application”?

  • GutterBoy March 7, 2013, 5:34 am

    Good morning Mr Trevor Page-From Ireland :)
    ———————————————————————————————————————————–
    DO I COPY AND PASTE THIS CODE?
    ———————————————————————————————————————————–

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

    ———————————————————————————————————————————–
    COMPLETELY OVER THIS CODE?
    ———————————————————————————————————————————–

    package com.helloworld;
    
    public class HelloWorld
    {
    }
    
    • Trevor Page March 7, 2013, 7:57 am

      You got it!

      • GutterBoy March 7, 2013, 11:50 am

        :)

      • GutterBoy March 7, 2013, 3:56 pm

        Yay it worked.My first Java App-Once again thank you Master,this is the only site from now that im gonna study on-Keep it up Mr Trevor Page.

        • Trevor Page March 7, 2013, 5:28 pm

          Great to hear that it worked. Keep at it, I know from first hand experience that it can be frustrating, but hard work pays off.

          • GutterBoy March 8, 2013, 9:34 am

            Ok thx for motivating me.

  • Owell April 12, 2013, 9:52 am

    Hi, love your tutorial.I have being using NetBean IDE 6.5M1 version on my system. What should be done when project folder cannot be created while starting a new project ?

  • omuse oludare April 22, 2013, 5:29 pm

    I so much appreciate the simplicity with which you explain some basic concept in java programming. However , i have some issues with the start off in the package. I have downloaded java JDK-7u17-i586 compiler on my 32bit laptop, but I find it difficult to arrive at the window where i will actually type in my codes in java. Can you guide me? Thanks .

    • Trevor Page April 22, 2013, 7:56 pm

      Have you downloaded an IDE yet? Like Spring Tool Suite or Eclipse?

  • abhishek May 26, 2013, 10:19 am

    OMG!!!
    that’s really awesome , i’ve never thought programming can be such easier
    thank you really very much for posting this , i’ve tried very different sites and books but the language was never such easy …
    really thankful for this
    umm..
    one more thing can you please post link of your video tutorials ,
    tysm

  • saran July 1, 2013, 5:34 am

    Hi Trevor,
    i like the above postings its really very helpful to me because am beginner and the way of explanation is really very nice. and i have one doubt that i am using JDK 7 in which i am not using the package which you have mentioned in first line, can you please tell me is there any difference between those things and runs program with cmd.

    • Trevor Page July 1, 2013, 11:25 am

      JDK 7 will work just fine, it’s just one version higher than 1.6 (aka. JDK 6)

  • Jacqui January 5, 2014, 3:14 am

    Wow I love this. You have got me off to a good start with Java! Looking forward to learning more! I’m just currently having problems installing the JDK but I’m sure I’ll get through it soon… Thanks for making it all so clear and easily understandable… :-)

  • Ryan February 15, 2014, 6:02 pm

    Hello,
    Thank you for making this website. I am 13, and would like to be a computer programmer, but have not found a good tutorial for java that interests me until now. I especially like how you not only explain what to put into the code, but why to put it, and when you will need it. Some people say to do something but never even explain why I should do that. In this tutorial I am actually learning. I hope that you continue to keep up this website as many people are learning from it!

  • Chadi March 10, 2014, 6:49 am

    Hello,
    I would like to ask why i don’t have a dashboard in my springtool only thing i see is an error msg:

    Plug-in “org.springsource.ide.eclipse.commons.gettingstarted” was unable to instantiate class “org.springsource.ide.eclipse.commons.gettingstarted.dashboard.WelcomeDashboard”.

    What i should do?
    Thank you,

    • Trevor Page March 19, 2014, 4:15 pm

      I personally wouldn’t worry about it, the dashboard is something that I disable/close right away whenever spring tool suite loads.

  • Abdelrahman June 28, 2014, 8:18 pm

    I don’t think I learnt something new in this episode, we just copied and pasted! Please tell me the fundamental of building such a simple code to write it all by my self!

  • Anonymous September 9, 2014, 9:19 pm

    I want to do this. How do you download the programming software?

Leave a Comment

Powered by sweet Captcha