21

What is an acceptable folder structure for Java projects in IntelliJ IDEA?

Multiple sources (like this) suggest the following structure:

.
│ .idea    
└── src
    ├── main
    │   ├── java   
    │   │   └── com.simpleproject
    │   │       └── SimpleClass.java
    │   └── resources
    └── test
        ├── java
        │   └── com.simpleproject
        │       └── SimpleClassTest.java
        └── resources

I know this has worked before, but right now it is complaining java.lang.SecurityException: Prohibited package name: java

Apparently, java is not allowed as a package name. I don't understand why it's sometimes acceptable and sometimes not acceptable. Can someone provide a complete example of an acceptable project folder structure in a Java project in IntelliJ IDEA?

Bitswazsky
  • 4,242
  • 3
  • 29
  • 58
Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89
  • 7
    In project structure, set your root folder (java source) at the java-level (likely, right now is set at the main-level) - I believe you can right-click on the main folder, set folder as (unset it first) and then right-click on the java folder, mark folder as 'source' – blurfus Jan 13 '17 at 16:02
  • 2
    Note that this isn't the structure natively used by IntelliJ. It's the standard structure for a Maven or Gradle project. Why don't you use one of those standard build tools? – JB Nizet Jan 13 '17 at 16:06
  • 2
    @ochi Thanks, turns out that setting `sources` to `main` instead of `java` caused the problem. – Atte Juvonen Jan 13 '17 at 16:08
  • 1
    That is a [standard maven directory layout](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html). I suppose you did not import your sources as a maven project, or you've created them manually. Either way the sources folders have not been configured correctly. Just open up the [project structure window](https://www.jetbrains.com/help/idea/2016.2/project-structure-dialog.html) (file -> project structure) and update your module. – Morfic Jan 13 '17 at 16:08
  • Also hava a look at https://stackoverflow.com/q/74203874/4134215 – Taimoor Khan Oct 27 '22 at 07:19

6 Answers6

36

That is the basic folder structure of a maven project. IntelliJ usually recognizes this and sets up sensical defaults for you.

If it didn't (or if it did but you modified them afterwards), you need to set up your java folder as the sources folder (i.e. the folder that contains the source code).

For this, you need to:

  1. Go to your project structure settings: File > Project Structure
  2. Select your project in the middle panel
  3. Select the 'sources' tab in the right panel
    • Note from a comment (thanks @Line): In IntelliJ 2018.3.5, you "select 'modules' tab in left panel".
  4. Navigate to the src/main/java folder and select it
  5. Mark it as Sources

Repeat for test folder (mark as 'Tests'), resources (mark as 'Resources'), test-resources (mark as 'Test Resources'), etc.

blurfus
  • 13,485
  • 8
  • 55
  • 61
4

Your configuration in the IntelliJ's File > Project Structure page will be overridden by the project's pom.xml after every clean install. To prevent this, you need to configure the source directory in pom.xml as below:

<sourceDirectory>src/main/java</sourceDirectory>
Umut Uzun
  • 807
  • 11
  • 13
1

In Project Structure Settings,select Modules -> choose any of the mark as to set the folder type to be of specific type. Then do mvn clean and mvn compile

enter image description here

enter image description here

Arpan Saini
  • 4,623
  • 1
  • 42
  • 50
0

Simply close intellij and delete any existing .IML file from the project root will do it

0

Am using Mac OSx, In my case the IntelliJ did not created the src/main/java directories for me and i tried to create those directories but i cannot see create directory under right click menu of the project.

Hence i created those directories manually via terminal / finder. Then i went to File -> Project Structure and marked src/main/java as sources

0

Let's say you have a multi-module project of Maven and you want to open it in IntelliJ, you click:

  1. File->Open and find and select the root pom.xml
  2. IntelliJ ask you the way you want to open it, select "Open as project"
  3. IntelliJ then open and index on it, during this time on the right lower corner a small popup will ask if you want to enable auto-import maven sub-modules, you must allow it to automatically import submodules,
  4. IntelliJ will then import each of the maven sub-modules of your project, and perfectly map the /src/main/java as source folder and etc.
SebastianX
  • 142
  • 1
  • 5