Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile java class in Intellij Idea due to "cannot find symbol class X" error

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

like image 438
humkins Avatar asked Dec 01 '13 16:12

humkins


People also ask

How do you fix Cannot find symbol mean in Java?

In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

What does a Cannot find symbol or Cannot resolve symbol error mean?

The cannot find symbol error, also found under the names of symbol not found and cannot resolve symbol , is a Java compile-time error which emerges whenever there is an identifier in the source code which the compiler is unable to work out what it refers to.

Why can't I run Java on IntelliJ?

Create a project, then create a Java module. Put your class there and execute it. If you only want/need the IDE for editing the Java file, edit the file accordingly, then compile it using javac and execute the class using java WhateverTheNameOfYourClass .


1 Answers

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.

like image 54
Michał Rybak Avatar answered Sep 26 '22 12:09

Michał Rybak