src folder not created when creating simple Intellij java gradle Project

JavaGradleIntellij Idea

Java Problem Overview


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 :-)

Java Solutions


Solution 1 - Java

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".

Solution 2 - Java

Just right create on your project directory and choose New -> Directory -> (choose all four to be created: src/main/java, src/main/resource, src/test/java, and src/test/resource) -> enter

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

Solution 3 - Java

@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"

Solution 4 - Java

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

Solution 5 - Java

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).

Solution 6 - Java

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).

Solution 7 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmcfly softView Question on Stackoverflow
Solution 1 - Javamcfly softView Answer on Stackoverflow
Solution 2 - JavaASBView Answer on Stackoverflow
Solution 3 - Javauser3133668View Answer on Stackoverflow
Solution 4 - JavaRichard ShinView Answer on Stackoverflow
Solution 5 - JavaTri DawnView Answer on Stackoverflow
Solution 6 - JavajustAnotherGuyView Answer on Stackoverflow
Solution 7 - JavasososockeView Answer on Stackoverflow