25

I created new Java module from scratch in my project. "create 'src' folder" was selected in module creation wizard. There is single "com" package in source folder which contains two public classes (say A and B). A class instantiates B class. IDE does not show errors and B class source is opened on "ctrl+B" pressing when cursor is located on the class name inside A class source. But when I'm trying to compile A class (by context menu -> "compile A.java class") following error is shown

Error:(15, 20) cannot find symbol class B

"src" folder is marked as source root.

It looks like IDE bug.

Someone suggested to do "File->Invalidate Caches" to fix alike issue also asked on SO. I tried that and it did not help.

What possible reason of that and how it can be fixed?

--

Intellij Idea Ultimate 11.1.5.

IDE Java: 1.7.0_45

Project SDK: 1.6.0_45

humkins
  • 9,635
  • 11
  • 57
  • 75

7 Answers7

30

Try compiling the whole project (or at least class B) first.

If you compile only class A, class B cannot be found because it hasn't been compiled yet (therefore, no bytecode B.class file exists).

This is not intuitive, but true. IDEA doesn't automatically compile dependant classes when you compile single class.

Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
  • I compiled B class first and then A class was successfully compiled. Before this I was sure all dependencies are compiled automatically as it is done when code is compiled using "javac" command line tool. Earlier my B class was located in different package than A and Idea shows another error: "package 'foo' does not exist". Obviously similar reason - no package folder. Thank you! – humkins Dec 01 '13 at 19:40
  • Thanks for your answer, it save my so much time – Ishikawa Yoshi May 07 '14 at 17:50
  • 1
    The above solution is not practical will enterprise projects with 1000s of classes. Rebuild worked for me. – Raj Rajeshwar Singh Rathore Dec 01 '20 at 17:45
21

"right-click on project -> compile"

This did solve the same problem for me.

Vouze
  • 1,678
  • 17
  • 10
  • 5
    I had to do "right-click on project -> rebuild" to solve the problem on my computer. Perhaps they changed the name of the option "compile" to "rebuild" in some iteration of IntelliJ. – Max Oct 07 '17 at 22:45
16

Select Build -> Rebuild Project, it works for me!

2

If a class within a package is dependent on another class in the package, you need to create a classpath. Otherwise, the compiler does not know that the other class actually exists. Your IDE probably knows inherently that the classes are in the same package, which is why it isn't showing any errors.

You can resolve this issue most simply by supplying both classes as arguments to the compiler (I'm not familiar with the tool you're using to compile so this may not be possible), or by compiling a class outside the package that imports package 'com'. Both of these will create the classpath file along with the class files.

Qoph
  • 43
  • 1
  • 6
1

Right click project: Maven -> Reimport

This worked on Intellij Mac.

kfkhalili
  • 996
  • 1
  • 11
  • 24
1

For me invalidate cache and reload project worked. See steps below.

on Mac

file > Invalidate caches.. > Invalidate and restart

Then go to the pom.xml file that has the issue,

CMD + SHIFT + A > RELOAD PROJECT
Zeus
  • 6,386
  • 6
  • 54
  • 89
0

If the problem can not revolve by invalid retart, or rebuild projet. however the maven clean install build well.

Sometimes, the cause is tomcat's tasks. If ur maven build well, then there is no reason that error cant find symbol. So you can edit tomcat conf, check if there is task before launch was setting, just delet these tasks. Tomcat will only take the maven build.

kk.
  • 3,747
  • 12
  • 36
  • 67