Reputation: 766
For some reason my Intellij IDEA IDE started highlighting the use of anything from outside the local class as an error. It only does it for items in the packages I wrote - for example, packages like java.* do not show as errors. Even the joda-time dependency has no error highlights, it's just from my own packages. The problem only started occuring after I wrote a pom.xml for the project, so I'm assuming it has something to do with maven.
For example, the main
of the following line is red with the error cannot resolve symbol 'main'
:
import main.java.com.jamobox.jamchatserver.clients.ClientReader;
Then in instances where a class is given as a parameter, e.g.
public void doSomething(Client c) { .. }
and I put something like
doSomething(new Client());
It will highlight the value in the parameters in red saying
doSomething(Client) cannot be applied to doSomething(main.java.com.jamobox.jamchatserver.clients.Client)
As I said the code compiles and runs absolutely fine, its just the IDE that thinks something is wrong. I have tried pretty much all of the answers given in this similar question, but nothing seems to have worked.
Anyone have any ideas on how to fix this?
In case it helps anyone figure this out, here is my current pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jamobox.jamchatserver</groupId>
<artifactId>JamChatServer</artifactId>
<name>JamChat Server</name>
<version>0.2.1</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
<type>jar</type>
<optional>false</optional>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Upvotes: 0
Views: 1418
Reputation: 31936
When you say "it compiles fine", you do not indicate how you are compiling? Based on what you are describing, and what I suspect the issue is, I'm assuming you are compiling via maven.
It appears as if there is some mismatch in the declared source directory and the package names. When you create a (bare-bones) java project through IDEA, it makes the source directory {project.basedir}/src
. Notice in the project view, the src
directory is blue indicating it is a main (i.e. production) source directory:
This is also shown in the project structure dialog:
For a maven project, the default main (i.e.production) source directory is java
in the path {project.basedir}/src/main/java
. There is then a corresponding test source directory {project.basedir}/src/test/java
. IDEA marks test source directories as green folders. Finally, there are both main and test resources
directories that are used for things like configuration files, images, etc. In IDEA 12.x resources
directories were marked as source and test-source directories with the same color as the corresponding java
directory. In IDEA 13 (currently in beta and to be released in December) they are marked as distinct resource directories.
It sounds like when you converted to maven, the main\java
directory was added to the src
directory and the com
root package directory was moved to java
, but when this happened, the package declarations where changed from com.jamobox...
to main.java.com.janbobox...
. This likely happened because when you made the move, src
was still configured as the source directory. So IDEA saw the package name change from com.jamobox...
to main.java.com.janbobox...
and change import statements (and likely package statements) to match.
To fix, you need to do the following:
java
directory (and not the src
directory) is marked as the project source as shown in the above screenshot (showing both IDEA 12.x and 13.x project trees)
com
root package directory is in src\main\java
and not src
package main.java.com
strings with package com
import main.java.com
strings with import com
That should fix the issues. If there is still red, run File > Invalidate Caches and restart IDEA. If after that there are still issues, please edit your original question and add screenshots of your project tree and the File > Project Structure > [Modules] > [Sources Tab] dialog window.
Upvotes: 5