First application in Java

Last Updated on

Time needed: 15 minutes

In this tutorial we'll write our first Java application. Before we do that, however, we need to download and install few tools.

Java Development Kit

To create applications in Java we need Java Development Kit, which is a collection of tools necessary for that task. You can grab a JDK here. Feel free to select any stable version. Currently the newest one is JDK 12. while the most used one is still JDK 8.

Tutorials available on this site treat the JDK 8 as the base version, that is, all the syntax from the provided examples will match the JDK 8, and if the code uses some features from next releases, it will be explicitly mentioned.

What's the difference between JDK and JRE?

JRE, which stands for Java Runtime Environment, allows to run Java applications. Because of that, it is often already available on personal computers. It contains a Java Virtual Machine, on which Java applications are running, as well as a collection of standard libraries that programs use, and a java command line tool to launch them.

JDK allows to run and develop Java applications. It contains JRE and additional tools, which are useful only for programmers.

Integrated Development Environment

Technically, applications in Java can be written in any text editor and compiled using the tools bundled with the Java Development Kit, but programmers typically use specialized software for that, called Integrated Development Environments (IDEs). If you're already curios how it looks like to build an application without an IDE, it's been described here.

IDEs are applications that greatly simplify programming, by providing syntax completion, static code analysis, code generation, among other things.

I recommend downloading IntelliJ IDEA Community Version, which is free, powerful and very actively maintained. Installation and configuration is out of the scope of this tutorial, so please refer to the guide here.

Once you have it installed, select File | New | Project...

If the Project SDK field is empty, you'll have to provide a location to your Java Development Kit. Select the Java project type from the list on the left and press next. Don't use any template, name your project HelloWorld, and press finish.

Now your project is ready, but it's empty. We'll add some code. On the left side there should be a panel showing your project structure. Inside, there should be a src folder. This is where all the code will go.

Right click on it, and press New | Java Class. Name your class HelloWorld.

It's empty and cannot be executed by our IDE. If you do so, by clicking on the green arrow in the top right corner, you'll get the following warning:

Error: Main method not found in class HelloWorld, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Let's add some code to change it.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world");
    }
}

Now we'll see the following output:

Hello World
Process finished with exit code 0

Leave a Reply