How to create minimal Java App from scratch

In this article we’ll guide you through building a minimal java application from scratch. You’ll need a basic knowledge of Java program language.

Note
The hello-world.zip contains source code of the application that we’re going to build.

Install Java Development Kit (JDK)

Since we’re going to build application we’ll need a development kit - programs and libraries to compile our code to a Java application.

  • As example you can install JDK on Ubuntu by typing apt-get install default-jdk
  • Windows users can find installation packages at Microsoft Build of OpenJDK

To make sure that JDK is installed you can type java -version in your terminal.

java -version

On my machine where I’m running Ubuntu and the output is the following:

openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13  +8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.  20.04, mixed mode, sharing)

Create the app

Create a folder for our app and navigate into it:

mkdir hello-world && cd hello-world

Create App.java file by typing or using context menu.

cat > App.java

Type or paste the following code:

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

Compile our app using javac Java Compiler. This operation will translate our code that we can read to the code that Java Runtime Environment (JRE) can execute. JRE is a program that can run Java programs. It contains Java package classes, runtime libraries and Java Virtual Machine JVM.

javac -d . App.java

Run the app

Type ls to list files in the in the hello-world. There will be two files:

App.class App.java

Now, we can run our app using JRE by typing:

java -classpath . App

The output is going to be the following:

Hello, World!

The second parameter -classpath . means that Java should search for the class files in the current directory. We also should pass the class name App that contains main method.

Create executable JAR file

In the next step we’ll create an executable Java Archive File (JAR) file. The JAR file contains all the resources that are needed to run the app such as class files, configuration files, images, and other resource files.

The only file we need to create is a Manifest file. It is a configuration file that will contain information about a class that contains main method.

Create App.mf manifest file by typing:

cat > App.mf

And pasting the following text:

Manifest-Version: 1.0
Main-Class: App

When the JRE will execute our application it will look for the class name that contains application entry point main method. In our case the entry point is in App class.

We’re ready to create our JAR file, type the following

jar cmf App.mf App.jar App.class App.java

This command will create App.jar file and place it in the current directory. Let’s now run the JAR file that we’ve created by typing.

java -jar App.jar

The output is going to be the following:

Hello, World!

The file App.jar can be shared and executed on another machine with JRE installed.

Summary

In this article we’ve created sample Java application that prints “Hello, World!” to the console.