Basic Setup - Installing the Java tools for the tutorials

How to import a Gradle project into Eclipse or IntelliJ

In the JCoder website, most of our tutorials make use of Gradle as the build system for our projects. Using a build system makes it easier to manage your Java project and gives readers a standard approach in all of our tutorials.

As not all readers know about Gradle, we’ve put together this tutorial where we’ll show how to import a Gradle project into Eclipse and IntelliJ.

Video Tutorial for Eclipse

In case you prefer the video version instead of the text version we’ve prepared 2 videos for importing Gradle projects into Eclipse:

  • a short version for people already familiar with the topic and,

  • a full version with description of the process as we follow it.

In case you prefer the text version, you’ll find the details after the videos.

The short (TL;DR) video version:

And our full video version with description of the import process:

Downloading an example project

For this exercise I’d recommend either downloading or cloning the example Java project using the links on the right hand side of this page, under the Code in GitHub section. Once you have the code in your machine, you’ll find a project with the following structure:

  1. build.gradle: This is the main configuration file for our Gradle project and is what defines how our build process will look like for our project.

    Inside that file, you’ll see we apply a plugin: apply plugin: 'java' that prepares a lot of the heavy-lifting for us and configures our Gradle project as a Java project.

    For more details, you can check the Java Gradle plugin page here.

  2. src/main/java: This is the folder where we’ll add our Java files.

Other files and folders will be covered in later tutorials and courses.

Importing the project into Eclipse

The Buildship Eclipse plugin

In the most recent versions of Eclipse, the Buildship plugin comes installed by default.

If you have an older version of Eclipse you might need to install it manually.

This plugin is the one that provides Gradle support for Eclipse.

Importing the project

To start the importing process, you can either a) select the FileImport…​ option or b) right-click inside the Package Explorer tab (anywhere inside the tab) and click Import…​ in the context menu that appears.

In the Import dialog, select GradleExisting Gradle Project and click Next

You’ll then be shown a wizard to import the project:

  1. The first screen is a welcome screen, so no input required here, click Next

  2. In the second step, the wizard will ask us for the Project Root, this would be the folder where you downloaded the Example project at the beginning of this section. Make sure you select the folder that contains the build.gradle file and click Next.

  3. For the remaining steps you can leave the default options and proceed with the Import wizard or click Finish.

Checking the project

Once the project is imported, it should be displayed in the Package Explorer tab. You can expand the project to check the files. Look for the ExampleApplication.java file which should be inside the src/main/java folder under the (default package) and double-click on it.

You should be seeing something like this:

Java Basics Imported to Eclipse

Compiling the project

Remember the javac command? Eclipse should be running this for you behind the scenes automatically, so you don’t need to worry about this for the example projects we’ll be covering.

This means your project is being built/compiled as you add/change/remove code.

Running the ExampleApplication Java application

Inside of the Package Explorer tab, right-click on the ExampleApplication.java file. You should be offered a context menu that has a Run As option, hover over it and then click on Java Application.

If everything worked ok, Eclipse should open a Console view on the bottom with the output of our application.

ExampleApplication in Eclipse

Importing the project into IntelliJ

When running IntelliJ for the first time as shown in the previous section, IntelliJ will display a Welcome screen with an Import Project option. If you’ve already imported/created a project before, you might need to click on FileClose Project to go back to the Welcome screen.

Select that option and select the root folder where you downloaded the example project. Make sure you select the folder that contains the build.gradle file.

In the Import wizard, select the following options:

  1. Select Import project from external model and select Gradle as the option as this is a Gradle project.

  2. In the next step, make sure the Gradle project folder is pointing to the folder that has the build.gradle file.

You can leave the other options with the defaults values and click Finish.

Once the project is imported, IntelliJ should open it automatically. You can expand the project to check the files. Look for the ExampleApplication file which should be inside the src/main/java folder and double-click on it.

You should be seeing something like this:

Java Basics Imported to IntelliJ

Compiling the project

You can compile/build the project via the Build menu or just by pressing Ctrl+F9 (shortcut in Mac might be different).

Running the Example

Inside of the ExampleApplication file, right click anywhere in the text and in the Context menu select the Run ExampleApplication main() option.

If everything worked ok, IntelliJ should display a new section on the bottom with the details of the recent execution (Run window) and you should be able to see the output of the application, for example:

ExampleApplication in IntelliJ
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step