JamoBox
JamoBox

Reputation: 766

Intellij highlights members of other classes as errors, yet it compiles fine

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

Answers (1)

Javaru
Javaru

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:

enter image description here

This is also shown in the project structure dialog:

enter image description here

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.

IDEA Project Tree

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:

  1. Force import the maven pom using the "Reimport All Maven Projects" button Reimport All Maven Projects button in the Maven tool window (on the right side by default)
    • If the POM is not yet showing, click the add button add button, navigate to the pom.xml file and add it. Then run the reimport.
  2. Verify the 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)
    • If the proper directories are not marked, try a reimport 2 or 3 more times. If that does not fix it (it should), go into File > Project Structure > [Modules] > [Sources Tab] and manually configure the proper sources. Remove any extraneous ones.
  3. Verify that the com root package directory is in src\main\java and not src
  4. Search and replace any package main.java.com strings with package com
  5. Search and replace any 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

Related Questions