How to create a java project with gradle in eclipse

Posted on April 19, 2017

In this tutorial, we will learn how to create a java application using the Gradle build tool and Eclipse IDE.

Tools and technologies used-

  • JDK1.8.0_71
  • Eclipse Neon.3
  • Buildship, Eclipse Plug-ins for Gradle
  • Gradle 3.4
  • JUnit 4.12

Let’s begin step by step to see how a gradle java project is created in eclipse.

Install Gradle plug-ins (Buildship)

You can install the Buildship gradle tool from Eclipse Marketplace. Go to HelpEclipse Marketplace… menu to install the Buildship gradle tool.


Create Gradle project

In Eclipse, go to FileNewOther… menu path to select new project.

In New wizard, select Gradle Project and click on Next.


In next wizard, type the Project Name and click on Next.


In next wizard, select gradle distribution and click on Next.


In next wizard, preview the project configuration and click on Finish.



Review project structure

Review the following project structure generated by the Gradle build tool.


Gradle build file - build.gradle

Open build.gradle file and add the following code in it.


apply plugin: 'java'
apply plugin: 'application'

repositories {

dependencies {
   compile ''  // Google Guava dependency
   testCompile 'junit:junit:4.12'         // JUnit dependency for testing

mainClassName = 'com.boraji.tutorial.MainApp'  // Main class with main method

Refresh the Gradle Tasks view after changes in the build.gradle file as follows.


Create Java programs

Remove the java files, generated by the gradle build tool, from project and create new files as follows.

Create file under src/main/java folder and write the following code in it.

package com.boraji.tutorial;

 * @author imssbora
public class MainApp {

   public String sayHello() {
      return "Hello Gradle";

   public static void main(String[] args) {
      MainApp app = new MainApp();

Create file under src/test/java folder and write the following JUnit testcase in it.

package com.boraji.tutorial;

import org.junit.Test;
import static org.junit.Assert.*;

 * @author imssbora
public class MainAppTest {

   public void testSayHello() {
      MainApp app = new MainApp();
      assertNotNull("Success", app.sayHello());

Build project

In Gradle Tasks view/tab, right click on the build task and select Run Gradle Tasks to build the java project.


Console output-

Gradle Tasks: build

:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_121 contains a valid JDK installation.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Total time: 20.754 secs

As you can see in the console output, the project build is failed because the gradle build tool did not find the JDK environment.

To resolve the above error, create file under project’s root folder and write the following property (i.e. java home path) in it.\\Program Files\\Java\\jdk1.8.0_71

Now execute the build task again. After the build is finished, the console output looks like as follows.

Gradle Tasks: build

:processResources NO-SOURCE
:processTestResources NO-SOURCE


Total time: 4.911 secs


Review final project Structure

The final project structure of gradle java project will look like as follows.


Run project

In Gradle Tasks view, right click on the run task and select Run Gradle Tasks to run the java project.


Console output-

Gradle Tasks: run

:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
Hello Gradle


Total time: 0.758 secs

Run JUnit testcase

In Gradle Tasks view, right click on the test task and select Run Gradle Tasks to run the JUnit testcase.


Console output-

Gradle Tasks: test

:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test UP-TO-DATE


Total time: 0.277 secs