1

From this post (Best way to add Gradle support to IntelliJ Project) I can see that what I need to do is "Add build.gradle in your project's root directory."

I was hoping someone could please explain how I do that in intelliJ? (been searching and reading - still baffled).

I understand a root directory the folder which is the parent for all the project sources, but in standard JavaFX project in intelliJ what is it/how do I find it/assign it, and then how do I add build.gradle?

1 Answer 1

1

Note: these steps assume that you are using the latest JDK version (17).

In the project browser, right click on the project name and create a file named build.gradle:

Creating build.gradle

Write a build script. Here is a template for JavaFX applications:

plugins { id "application" // Use Application plugin id "org.openjfx.javafxplugin" version "0.0.9" // Use JavaFX plugin } mainClassName = "my.package.Application" // Set this to your main class repositories { mavenCentral() } javafx { version = "16" // JavaFX Version modules = [ "javafx.controls" ] // JavaFX modules. Add them to this array. } 

Once you have finished, import the project:

Importing the project

This may take a while depending on your internet speed.

You may have to adjust runtime configurations to use Gradle, but that shouldn't be necessary.

You may encounter the following error:

BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61 

If that is the case, install Gradle CLI (if you haven't already), and open a command prompt/terminal in the project folder.

Run the following command (works on Windows/Mac/Linux):

gradle wrapper --gradle-version=7.3 --distribution-type=bin 

Once you have done that, reload the Gradle project.

I could not resist another GIF:

Reloading Gradle project

You may also need to rearrange the source folders:

enter image description here

I apologise for the cat photo, but it was just a placeholder image. I definitely didn't specifically choose it.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot for putting that answer together. I followed the steps and changed the class name in your buildscript to my Main, but I get the following error: "Could not open init generic class cache for initialization script...BUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 60"... I'm way out of my depth here
That's the problem I had. I just assumed that it was an issue with my machine. I fixed it a few weeks ago, but forgot how. I'll just try and find what I did with my other project to get it working.
I have added a fix to the answer.
That got it! Absolute legend, thanks. Must be a gradle version/JDK compatibility issue. Despite not changing the JDK version I actually had to update IntelliJ before it worked as well, not sure why. But it worked. It has brought up an error running Main now, but I assume that's due to the project structure needing to be what gradle expects- I can have a look at that. Cheers for your help, much appreciated :)
Ah never mind re what I said about Main I missed your last gif on source folders. Thanks a lot! I enjoyed the cat...no worries

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.