Java Hello World – How to Run

By , last updated July 16, 2018

In this article we will show you how to start programming in Java from scratch. We will use Java 9 for this beginner tutorial and write and run a “Hello, World!” example program on your computer.

Installing Java

In order to start programming in Java you will need to install Java on your computer.

Step 1. Install JDK
Go to the Oracle website and download and install the latest JDK pack. We used Java 9 JDK. JDK stands for Java Development Kit and is a library that you need to compile and run Java applications.

Step 2. Provide JAVA_HOME

JAVA_HOME is a system variable that tells your system where Java JDK is installed on your computer.

Open System Variables by choosing Start -> Control Panel -> System -> Advanced system settings -> Tab (Advanced) -> Environment Variables

Add a new variable called “JAVA_HOME” and the variable value will be the path to your installed JDK:

Where to write Java code

There are several ways to write and run Java code. The preferred one is to use IDE – Integrated Development Environment.

Choosing IDE for Java programming

There are several good IDE’s for Java development:

  1. Eclipse is an easier and open source IDE for Java. Eclipse is our tool of choice for this tutorial as it’s totally open source and doesn’t have any restrictions in case you need some extra features.
  2. IntelliJ is the most popular IDE among many fellow developers at work. It has a free community edition and a paid commercial one. A free edition is good, but is also very limited.

We have downloaded and installed Eclipse for the purpose of this article:

Writing Java “Hello, World!” program

Here are the steps to creating and writing a simple “Hello, World” program in Java 9 using Eclipse IDE:

  1. Create Java project in Eclipse. Go File -> New Java Project. Under JRE choose “Use execution environment JRE: JavaSE-9”.
    See in the errors section at the end of the article if you get some problems down the line.
  2. Add a new Java file into our new “helloworld” project. Right click on src -> New -> Class.
  3. Name the class “HelloWorld”:
  4. Write the code. Open the new HelloWorld.java file by double clicking on it and write the following “Hello, World” code:
    package helloworld;
    
    public class HelloWorld {
    	public static void main(String[] args) {
            System.out.println("Hello, World");
        }
    }
    
  5. Run the code by clicking the green circular button at the top or clicking Ctrl+F11. You should see the words “Hello, World!” in the console:

Run Java code from a command line

It is possible to compile and run simple Java files from a command line. Here’s how to do it in Windows:

Step 1. Enable Java for your system. In order for your command line to have access to Java commands like javac and others you need to tell your computer where your JDK is located.

Open Environment variables and add the path to your JDK bin folder to the PATH variable:

Restart CMD.

If you don’t do this step, you may get an error “javac is not recognized as an internal or external command”.

Step 2. Write the following code in your editor of choice and save it as HelloWorld.java file:

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

Step 3. Compile the Java-file to a class-file:

javac HelloWorld.java

Step 4. Execute the new Java class-file:

java HelloWorld

The output should be “Hello, World from command line!”

Eclipse errors

We have encountered some errors in Eclipse while trying to create and run the project for the first time. Here are some of them:

Unbound classpath container: ‘JRE System Library [JavaSE-9]’ in project ‘helloworld’

We were using Java 8 prior to this tutorial and the choice of Java 9 didn’t go through. To fix it, add Java 9 JRE into Installed JREs in Eclipse.

Choose Window -> Java -> Installed JREs -> Add -> Standard VM and add jre-9xxx folder into JRE home field.

Senior Software Engineer developing all kinds of stuff.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*