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.
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:
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:
We have downloaded and installed Eclipse for the purpose of this article:
Here are the steps to creating and writing a simple “Hello, World” program in Java 9 using Eclipse IDE:
package helloworld; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
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
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!”
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.