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?
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.
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.
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.
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.
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.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With