Basic Java Application Project Using Gradle

Here is how to setup a Gradle build to run a basic Java project through the main method:

Create a folder for the project. In this example, we will name the folder basic-java.

Create a Java file, called HelloWorld.java, inside basic-java/src/main/java/com/codetutr: package com.codetutr;

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

Inside the project root folder (basic-java), create your Gradle build script file:

build.gradle
apply plugin: ‘java’

task(runMain, dependsOn: 'classes', type: JavaExec) {
    main = 'com.codetutr.HelloWorld'
    classpath = sourceSets.main.runtimeClasspath
    args 'CodeTutr'
}

Notice that we are defining our own task called runMain and setting the class which contains the main method we would like to run

From the command line, navigate to your basic-java folder and run gradle runMain.

$ gradle runMain
:compileJava
:processResources UP-TO-DATE
:classes
:runMain
Hello, CodeTutr!

BUILD SUCCESSFUL

Total time: 3.437 secs

Full Source: basic-java.zip

Download, unzip, run gradle runMain.

Posted in Gradle Tagged with: ,
  • Isuru K

    Nice tutorial about gradle.

  • Sugiarto Cokrowibowo

    Terima Kasih tutorialnya…!!! Sangat membantu untuk pemula seperti saya… 😀

Sponsored

Follow Me

RSS FeedFollow me on Twitter