23

when I use the word "var" the IDE recognize the command, but when I compile the code, it gives me an error:

Error:(10, 17) java: cannot find symbol

symbol: class var

location: class Exp

the code:

public final class Exp
{
    public static void main(final String[] args)
    {
        var x=5;
    }
}

So why does it happen? How can i solve it?

the pics: the project SDK is 11 and so is the language level

The module language level is 11 also

The full warning

Community
  • 1
  • 1
Roy Ash
  • 1,078
  • 1
  • 11
  • 28
  • Have you properly set IntelliJ? – Gilberto T. Nov 04 '18 at 12:08
  • Are you sure you are using JDK 10 / 11 for compilation? – enveeed Nov 04 '18 at 12:09
  • 1
    The most likely explanation is that the Intellij project configs ... or Maven / Gradle / whatever build configs ... are not selecting Java 11 as your source level; see https://stackoverflow.com/questions/29915259/how-to-change-project-language-level-for-all-project-in-intellij – Stephen C Nov 04 '18 at 12:11
  • My project language level is 11 and i'm using JDK 11 in the build output is says: > Information:javac 11.0.1 was used to compile java sources – Roy Ash Nov 04 '18 at 12:16
  • Well clearly the compiler you are actually using *thinks* that the source level is 9 or less. Otherwise it would realize that `var` in that context is not a class name. Did you check **all** of the things I suggested? – Stephen C Nov 04 '18 at 12:48
  • Yes.. if you want you can check it yourself somehow.. can I upload some screenshots to this conversation? – Roy Ash Nov 04 '18 at 12:55
  • No ... I don't. I trust you have checked it yourself properly. (And it is no skin off my nose if you didn't :-) ) – Stephen C Nov 04 '18 at 13:24
  • 2
    Additionally, to note, you might be compiling with javac from JDK 11 but executing with java from JDK of version lower than 10, since the error reads `Error:(10, 17)` **`java`** `: cannot find symbol` – Naman Nov 04 '18 at 17:21
  • 3
    Are you using Maven? If not, navigate to `Settings->Build, Execution, Deployment, ->Compiler->Java Compiler` and see if you have a module that has a lower target bytecode version, although that doesn't seem to pose a problem for `var`. Alternatively, check if your **module** language level is set to 11 under `Project Structure->Project Settings->Modules->Language Level`. – Marv Nov 04 '18 at 17:25
  • I've edited the post and added some images for explanation.. – Roy Ash Nov 04 '18 at 21:15

1 Answers1

43

Thanks to @Marv the solution was

Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler

and raise the Project Bytecode version to 11. (I raised it from 8 to 11)

Pic: Go to Project bytecode version and raise it to max level

Roy Ash
  • 1,078
  • 1
  • 11
  • 28
  • 1
    Related https://stackoverflow.com/questions/53053587/how-to-avoid-intellij-to-reset-language-level – Naman Nov 05 '18 at 02:06