54

I am following the instructions to create a simple IntelliJ Project with Gradle support and the src folder is not created (I guess this is the base of any Java Project) https://www.jetbrains.com/help/idea/getting-started-with-gradle.html

  1. New Project: I check Gradle and Java - next
  2. Added Artefact ID -> next
  3. "Use auto-import", "using explicit module groups", "create separate Module per source set", "Use default Gradle wrapper" -> next
  4. Give a project name -> next

I have no src folder. I am not able to add a new class.

enter image description here

When doing the same without gradle I have the src folder.

UPDATE Adding a module called "src" works, but then I have a module called src and a folder in it called src. But this is a hack. Just bought IntelliJ for 200 bucks and I hope this can be solved in a more professional way :-)

7
  • 1
    Why do you add a "module"? Just add the directory.
    – JB Nizet
    Apr 7, 2018 at 18:55
  • What do you see when clicking on the little arrow next to Project
    – baao
    Apr 7, 2018 at 19:42
  • @baao: I see what you can see in the screenshot. Project is already opened in the Screenshot.
    – mcfly soft
    Apr 7, 2018 at 21:12
  • 1
    src/main/java is also not there. That is not the point. It is not normal to manually add this folders. That is the point. See my answer. In my oppinion this should be a task for the IDE and not for the developer. I even was not able to add folders manually. I had to configure this hidden magical checkbox in my answer.
    – mcfly soft
    Apr 7, 2018 at 21:54
  • 1
    Sorry, there is a bug in 2018.1 version when the project structure for a new Gradle project is not created automatically: youtrack.jetbrains.com/issue/IDEA-189674 I hope it will be fixed soon.
    – Andrey
    Apr 8, 2018 at 11:25

7 Answers 7

65

For Windows the solution is. To go manually into: File -> Settings -> Build, Execution, Deployment -> Gradle -> check the "Create directories for empty content roots automatically".

For Mac the solution is. To go manually into: Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> check the "Create directories for empty content roots automatically".

5
  • 2
    On Mac, go to IntelliJ IDEA -> Preferences -> Build, Execution, Deployment -> Gradle. Couldn't agree more - it's stupid. If this is a bug as @Andrey suggests, hopefully it will be sorted soon.
    – Flic
    Apr 26, 2018 at 3:37
  • 4
    In IntelliJ 2018.1.5 it is in Build, Execution, Deployment -> Build Tools -> Gradle. Also the setting is a Project-level setting, that means it must be set per prorject and is not available from the start-screen when no project is opened.
    – Thommy
    Jun 28, 2018 at 6:26
  • What about Linux? Edit: It's the same as on Windows.
    – jv110
    Jul 23, 2018 at 4:09
  • 10
    Seems like this was removed in the recent 2019.2 update :(
    – npstr
    Sep 15, 2019 at 13:57
  • 5
    @npstr I also don't see this option. When I'm creating a new Gradle->Java project only build.gradle & settings.gradle file gets created in new empty project... How did you fix it... Jan 14, 2020 at 2:03
33

Just right-click on your project directory and choose New -> Directory.
Select all four to be created.

  1. src/main/java
  2. src/main/resource
  3. src/test/java
  4. src/test/resource

right click on project directory

choose the four choices

found the answer here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206806425-Source-Directories-Not-Being-Created-In-New-Project

1
12

@here A quick fix that worked for me. Once you have created the gradle project as mentioned in the question

  • open the gradle sidebar on the right hand side.
  • click settings
  • a new dialog box will appear.
  • In this dialog box check the option "create directories for empty contents roots automatically"
  • then click apply and src folders will be created for you ..

Images are attached for clarity.

Open the gradle sidebar on the right hand side.

Check the option "create directories for empty contents roots automatically"

2
  • what to do If I don't have a check option "create directories for empty contents roots automatically". Before it created the src folder without any problems;) Dec 17, 2019 at 1:38
  • This is the same as the accepted answer. Only the method of opening the settings differs, but it's same dialog
    – Agent_L
    Jul 12, 2021 at 17:13
5

As of January 2022 with Intellij IDEA 2021.1.3 and Gradle 6.8

First of all, right-click your project main folder ...

First Step

Go to > New > Directory...

Second Step

After clicking the "Directory" option, this will open a dialog containing available options of "Gradle Source Sets" to add.

Third Step

You can select what "Gradle Source Sets" you want to add. In my case, I have already added the "src/main/kotlin" (Notice it's not displayed in the options).

1
3

If enabling the "Create directories for empty content roots automatically" setting does not show the src directory, check the project structure and try creating and deleting a dummy module.

  1. File -> Project Structure -> Modules -> Project Name -> Confirm that src is part of the hierarchy and exit out of the pop up
  2. Right click on the root folder of the project -> New -> Module
  3. The IDE may have refreshed, along with the visibility of the src folder
2

None of the above worked for me. Finally, had to go raw.

gradle init --type java-library

Followed Mkyong's page on gradle init

Then, simply 'Open' project in Intellij. Then updated project settings related to JAVA_HOME and Gradle home (Chances are these were related to my specific installation).

1
  • This does not work unfortunately: > Task :init SKIPPED The build file 'build.gradle' already exists. Skipping build initialization.
    – wlfbck
    Feb 22, 2022 at 8:42
2

Add a directory named 'src'. Right click on it and then click Mark Directory as Sources Root.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.