[Java] Hello world with Java and Gradle

2 minute read


I don’t know what it is, but for learning, I created a program that uses Java and Gradle to display “Hello world.”


  • Java version
    • 11
  • Gradle version
    • 6.5.1
  • OS
    • Windows 10

We have confirmed the operation in the above environment. However, since it is only a very simple matter, you should be able to read some differences between the versions appropriately.

Creating a Gradle project

To create a Gradle project, first create an appropriate folder. The command is as follows.

mkdir hello-gradle
cd hello-gradle

Here I named the folder hello-gradle.

Next, create a project with the gradle command. Make the folder in which you want to create a project the current directory (that is, cd hello-gradle and stay in the folder) and execute the following command.

gradle init --type java-application

Then you will be asked a few questions.

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2]

Select whether to write the script that configures the build in Groovy or Kotlin. In case of Java project, it seems that Groovy is normally used, so enter “1” and press Enter.

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter

Then select the test framework. This time I just do Hello world, so any test is acceptable, but for now, enter “4” and press Enter.

Project name (default: hello-gradle):
Source package (default: hello.gradle):

Finally, you will be asked for the “Project name” and “Source package” respectively. Here we will proceed by default, and enter without entering anything.

This should generate various files in the hello-gradle folder. All the source code needed to do “Hello world.” is already in place. The main method that outputs the most important “Hello world.” as standard output is in src/main/java/hello/gradle/App.java.

In addition, I did gradle init --type java-application at first, but the same result should be obtained by just adding some questions even if you do not specify --type, just gradle init. However, it is faster to specify --type java-application when creating a Java command line application.

Types that can be specified in --type are described in Supported Gradle build types. For example, if you specify kotlin-application, it seems that you can make a Kotlin project.

Operation check

In the hello-gradle folder, at the command prompt,

gradlew.bat run

Execute the command. Then the string of Hello world. should be output. This completes the Hello world with Java + Gradle.

Run test

Since it is a program that only outputs Hello world as standard output, it does not make sense to test it, but since the test code is prepared, I will also execute the test.

As with the operation confirmation, if the command prompt,

gradlew.bat test

Run. The test will pass and you will see BUILD SUCCESSFUL.

The test code is in src/test/java/hello/gradle/AppTest.java. The appHasAGreeting method of this class is the test method. This test only verifies that the getGreeting method of the App class with the main method does not return null. This method returns the string Hello world., so the test will pass.

This getGreeting method, for example,

package hello.gradle;

public class App {
    public String getGreeting() {
        // return "Hello world.";
        return null;

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());

If I change it to return null like this and run the test, then the test fails and I get the word > Task :test FAILED and the test method that failed.