Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ: Source root is not handled correctly

I have the following project structure:

ModuleName (=Content Root)
  |---src
  |    |---com
  |         |---company
  |             |---file1.java
  |             |---file2.java
  |---test-src
  |    |---com
  |         |---company
  |             |---test.java
  |--- .classpath
  |--- .project

This was an Eclipse project initially, and I need to import it in IntelliJ.

However, everytime I try to import it, IntelliJ is completely confused with the source root folder.

Looking at the java source files, I get the following error in the package declaration:

package com.company;

Error: Package name 'com.company' does not correspond to the file path 'src/com.company'

and

Error: Package name 'com.company' does not correspond to the file path 'test-src/com.company'

I correctly marked the 'src' and 'test-src' folders as source-roots in IntelliJ. However, it always thinks that these folders are part of the package hierarchy.

I have this problem in 3 of about 30 different modules. All modules are structured the same, and I don't really see any reason why IntelliJ works for most of them, but fails for those three.

How can I resolve this?

like image 229
maja Avatar asked May 10 '17 17:05

maja


People also ask

What is sources root in IntelliJ?

Sources Root - this is where your actual project code goes - typically in src/main/scala (though you can change that, or add extra source directories in your build). Directories nested below this level should be packages. Intellij needs to know these are sources so it can highlight them, check them for errors, etc.

What is source root?

Source roots (or source folders; shown as ). These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports. The files under the source roots are interpreted according to their type.

What is .IML file in IntelliJ?

A module file (the . iml file) is used for keeping module configuration. Modules allow you to combine several technologies and frameworks in one application. In IntelliJ IDEA, you can create several modules for a project and each of them can be responsible for its own framework.

How do I find the content root directory in IntelliJ?

The content root directory in IntelliJ IDEA is marked with the icon. From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Project Settings | Modules. Select the necessary module and then open the Sources tab in the right-hand part of the dialog.

What is generated sources root in IntelliJ?

Generated Sources Root. The IDE considers that files in the Generated Sources root folder are generated automatically rather than written manually, and can be regenerated. ... IntelliJ IDEA marks the selected root as a regular folder; the folder itself and its contents won't be deleted.

Are the'SRC'and'test-SRC'folders source-roots in IntelliJ?

I correctly marked the 'src' and 'test-src' folders as source-roots in IntelliJ. However, it always thinks that these folders are part of the package hierarchy. I have this problem in 3 of about 30 different modules.

Do I need to manually mark source folders in IntelliJ?

There should be no need to mark individual source folders manually. I prefer to do a git clone outside of IntelliJ, and open the project from the local filesystem, then IntelliJ imports the maven project structure automatically. What’s the effective way to insert more a million rows into postgresql server from another postgres server using Java?


1 Answers

For completeness' sake - if anyone runs into the same problem with a similar setup, here is the solution:

Another module (let's call it module B) had a dependency on the .jar file generated by module A. This normally works perfectly fine, and the required .jar can be found in module B's lib folder. The issue was that module B's .classpath file (which was used to import the modules into IntelliJ) included the following entry (I assume it was generated by eclipse at some earlier point):

<classpathentry kind="lib" path="lib/ModuleA.jar" sourcepath="/ModuleA/" />

Now it seems like IntelliJ became extremely confused by this sourcepath entry (as it doesn't point to a valid location) and this somehow affected module A's packaging structure. What's interesting about this (and made it a huge pain to debug and find the actual issue) is that the error didn't manifest itself in module B where the problematic .classpath file was located (because IntelliJ doesn't show a warning that the sources imported from the .classpath file were invalid), but rather in the parent module A.

Removing the sourcepath entry (or pointing it to a valid location, i.e.: lib/ModuleA.jar) fixed the problem.

like image 95
RobinFood Avatar answered Sep 23 '22 18:09

RobinFood