Get error java cannot find symbol when I use "var" keyword with language level set to JDK 11

Answered

If I try compiling a Java 11 class using the var keyword I get a message saying: Error:(15, 13) java: cannot find symbol
symbol: class var
location: class ImageViewer

I am able to compile the class from the command line just fine.  Here is my code:

public class ImageViewer {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var frame = new ImageViewerFrame();
frame.setTitle("ImageViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}

/**
* A frame with a label to show an image.
*/

class ImageViewerFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 400;

public ImageViewerFrame() {
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// Use a label to display the images
var label = new JLabel();
add(label);

// Set up the file chooser
var chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

// Set up the menu bar
var menuBar = new JMenuBar();
setJMenuBar(menuBar);

var menu = new JMenu("File");
menuBar.add(menu);

var openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(event -> {
// Show file chooser dialog
int result = chooser.showOpenDialog(null);

// If file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION) {
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
});

var exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(event -> System.exit(0));
}
1
5 comments

Please check https://stackoverflow.com/questions/53140625/problem-in-compile-time-when-using-var-in-jdk-11

If issue remains attach screenshots of Settings(Preferences) | Build, Execution, Deployment | Compiler | Java Compiler settings and File | Project Structure | Modules | <module name> | Dependencies tab.

0

Thank you very much, Andrey, that did the trick!

-1

I keep running into this problem as well (2018.3.1) - rebuilding the offending class and restarting the process usually fixes it.

java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class var
location: class com.x.echo.resource.risk.PnlRiskSummaryResource

 



-1
Avatar
Permanently deleted user

The "Cannot find symbol" errors generally occur when you try to reference an undeclared variable in your code. A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

When your code is compiled, the compiler needs to work out what each and every identifier in your code means. As the compiler is going through the code it will find something and know what to do with it or not. Your Cannot find symbol error relates to the identifiers and means that Java cannot figure out what the "symbol" means.

The general causes for a Cannot find symbol error are things like:

  • Incorrect spelling.
  • Wrong case. Halo is different from halo.
  • Improper use of acceptable identifier values (letters, numbers, underscore, dollar sign), my-class is not the same as myclass.
  • No variable declaration or variable is outside of the scope you are referencing it in.
-3
Avatar
Permanently deleted user

I had a similar problem and I also had to open MyJavaProjectName.iml file and change 

<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8>

to

<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_11">

and then save the file.

1

Please sign in to leave a comment.