Hello world in Java and Gradle


I don't know what number it is, but for learning, I created a program that displays "Hello world." Using Java and Gradle.


We have confirmed the operation in the above environment. However, it's only a simple matter, so you should be able to read the slight differences in the versions as appropriate.

Creating a Gradle project

To create a Gradle project, first create a suitable folder. The command looks like this:

mkdir hello-gradle
cd hello-gradle

I named the folder hello-gradle here.

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

gradle init --type java-application

Then you will be asked some questions.

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

Select whether to write the script that sets the build in Groovy or Kotlin. For Java projects, you would normally use Groovy, so type "1" and press Enter.

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

Then select a test framework. This time it's just Hello world, so you can test anything, but for now, type "4" and press Enter.

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

Finally, you will be asked for "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 "Hello world." Is already in place. The main method that outputs the most important "Hello world." As standard is in src / main / java / hello / gradle / App.java.

In addition, I did gradle init --type java-application at first, but if you do not specify --type and just gradle init alone, you should get the same result with only a few more questions. However, if you want to create a Java command line application, it is faster to specify --type java-application.

The types that can be specified for --type are described in Supported Gradle build types. For example, it seems that you can create a Kotlin project by specifying kotlin-application.

Operation check

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

gradlew.bat run

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

Run the test

Since it is a program that only outputs Hello world as standard, there is no point in testing it, but since the test code is prepared, I will also run the test.

At the command prompt, just like when checking the operation

gradlew.bat test

To execute. The test will then succeed and you will see BUILD SUCCESS FUL.

The test code is in src / test / java / hello / gradle / AppTest.java. The ╩╗appHasAGreetingmethod of this class is the test method. This test only verifies that thegetGreeting method of the ╩╗App class, which has the main method, does not return null. This method returns the string Hello world., so of course the test passes.

For example, this getGreeting method

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 and run the test, then the test fails, with the words > Task: test FAILED and the failed test method.

Recommended Posts

Hello world in Java and Gradle
Hello World in Java
Hello World in java in eclipse now
Java, Hello, world!
Java Hello World
Minimal Java environment construction and Hello World
Java Learning (1)-Hello World
Hello world in node.js
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Build Java environment and output hello world [Beginner]
Until you install Gradle and output "Hello World"
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Java history in this world
java hello world, compile, run
Java beginners read Hello World
Hello World with SpringBoot / Gradle
Spring Boot Hello World in Eclipse
Encoding and Decoding example in Java
Hello World on AWS Lambda + Java
Hello world with Kotlin and JavaFX
Hello World with Docker and C
StringBuffer and StringBuilder Class in Java
Understanding equals and hashCode in Java
hello, world in Vanilla Java-EHW2018 "MVP"
Hello World with GWT 2.8.2 and Maven
Android OS (7.1.2) build and Hello World
In Java 10, when you do gradle eclipse and JavaSE-1.10 comes out, ...
Java Dekon'nichiwa world (Tsu learn quickly grammar in Vim and Quickrun)
Difference between final and Immutable in Java
Hello, World! In the bootstrap loader area
[Practice! ] Display Hello World in Spring Boot
Java development with Codenvy: Hello World! Run
"Hello, World!" With Kotlin + CLI in 5 minutes
Hello World on Mac VS Code Java
Hello world with Kotlin and Tornado FX
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
(IntelliJ + gradle) Hello World with Spring Boot
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
Partization in Java
Changes in Java 11
Read "Hello world"
Java and JavaScript
Gradle automatically generates version number from git and uses it in Java
XXE and Java
Hello Java Lambda
Pi in Java
Programming for the first time in my life Java 1st Hello World
FizzBuzz in Java
Regarding the transient modifier and serialization in Java
Output Hello World in kotlin's simple main function
Create barcodes and QR codes in Java PDF
Detect similar videos in Java and OpenCV rev.2
[Gradle] Build operations often performed in Java projects