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:
- 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.
- 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:
- 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. - Add a new Java file into our new “helloworld” project. Right click on src -> New -> Class.
- Name the class “HelloWorld”:
- 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”); } }
- 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:
## <span id="Run-Java-code-from-a-command-line">Run Java code from a command line</span>
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 <cocde>javac</code> 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:
<pre class="brush: java; title: ; notranslate" title="">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!”
## <span id="Eclipse-errors">Eclipse errors</span>
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.
See Also
- - Java 9 Strings
- - Java regex by example: strings
- - Java Basic Programs: 5 Simple Examples
- - Java variable types
- - How to sort a Set in Java example