Java import error cannot find symbol

What can cause a Cannot find symbol error in java?

Whenever you need to use external classes/interfaces (either user defined or, built-in) in the current program you need to import those classes in your current program using the import keyword.

But, while importing any class −

  • If the path of the class/interface you are importing is not available to JVM.
  • If the absolute class name you have mentioned at the import statement is not accurate (including packages and class name).
  • If you have imported the class/interface used.

You will get an exception saying “Cannot find symbol ……”

Example

In the following example we are trying to read a string value representing the name of the user from key-board (System.in). For this we are using the scanner class of the Java.Util Package.

public class ReadingdData < public static void main(String args[]) < System.out.println("Enter your name: "); Scanner sc = new Scanner(System.in); String name = sc.next(); System.out.println("Hello "+name); >>

Compile time error

Since we are using a class named Scanner in our program and haven’t imported it in our program. On executing, this program generates the following compile time error −

ReadingdData.java:6: error: cannot find symbol Scanner sc = new Scanner(System.in); ^ symbol: class Scanner location: class ReadingdData ReadingdData.java:6: error: cannot find symbol Scanner sc = new Scanner(System.in); ^ symbol: class Scanner location: class ReadingdData 2 errors

Solution

  • You need to set class path for the JAR file holding the required class interface.
  • Import the required class from the package using the import keyword. While importing you need to specify the absolute name (including the packages and sub packages) of the required class.

Example

import java.util.Scanner; public class ReadingdData < public static void main(String args[]) < System.out.println("Enter your name: "); Scanner sc = new Scanner(System.in); String name = sc.next(); System.out.println("Hello "+name); >>

Output

Enter your name: krishna Hello krishna

Источник

How to Resolve The Cannot Find Symbol Error in Java

How to Resolve The Cannot Find Symbol Error in Java

Symbol tables are an important data structure created and maintained by compilers to store information associated with identifiers [1] in a given source code. This information is entered into the symbol tables during lexical and syntax analysis and is used in the later phases of compilation. As the declarations of classes, interfaces, variables, and methods are processed, their identifiers are bound to corresponding entries in the symbol tables. When uses of these identifiers are encountered in the source code, the compiler looks them up in the symbol tables and relies on this information for things such as verifying that a variable has been declared, determining the scope of a variable, and verifying that an expression is semantically correct with type checking. Symbol tables are also used for code generation and optimization [2].

Читайте также:  Php get template directory url

A simplified representation of a symbol table entry (or simply, a symbol) in Java has the following format: . Given a global variable declaration like final double ratio; the corresponding symbol would then be .

Cannot Find Symbol Error

As its name implies, the cannot find symbol error refers to a symbol which cannot be found. While there are multiple ways and reasons this can occur, they all boil down to the fact that the Java compiler is unable to find the symbol associated with a given identifier.

The message produced by the compiler for the cannot find symbol error includes two additional fields:

  • “symbol”—the name and type of the referenced identifier; and
  • “location”—the specific class in which the identifier has been referenced.

What Causes the Cannot Find Symbol Error

The most common triggers for the cannot find symbol compile-time error include:

  • missing variable and method declarations;
  • out-of-scope references to variables and methods;
  • misspelled identifiers; and
  • omitted import statements.

Cannot Find Symbol vs Symbol Not Found vs Cannot Resolve Symbol

As different Java compilers use slightly different terminology, the cannot find symbol error can also be found under the terms symbol not found and cannot resolve symbol . Besides the naming, there is no difference between what these terms stand for.

Cannot Find Symbol Error Examples

Undeclared variable

When the Java compiler encounters a use of an identifier which it cannot find in the symbol table, it raises the cannot find symbol error. Consequently, the most common occurrence of this error is when there is a reference to an undeclared variable. Unlike some other languages that don’t require explicit declaration of variables [3], or may allow declaring a variable after it has been referenced (via hoisting [4]), Java requires declaring a variable before it can be used or referenced in any way.

Fig. 1(a) shows how an undeclared variable, in this case the identifier average on line 9, results in two instances of the cannot find symbol error, at the positions where they appear in the code. Declaring this variable by specifying its data type (or, alternatively, inferring its type with the var keyword in Java 10+) resolves the issue (Fig. 1(b)).

1 2 3 4 5 6 7 8 9 10 11 12
package rollbar; public class UndeclaredVariable < public static void main(String. args) < int x = 6; int y = 10; int z = 32; average = (x + y + z) / 3.0; // average is not declared System.out.println(average); >>
UndeclaredVariable.java:9: error: cannot find symbol average = (x + y + z) / 3.0; ^ symbol: variable average location: class UndeclaredVariable UndeclaredVariable.java:10: error: cannot find symbol System.out.println(average); ^ symbol: variable average location: class UndeclaredVariable 2 errors
1 2 3 4 5 6 7 8 9 10 11 12
package rollbar; public class UndeclaredVariable < public static void main(String. args) < int x = 6; int y = 10; int z = 32; double average = (x + y + z) / 3.0; System.out.println(average); >>

How to Handle the Expected Error in Java
Can Constructors Throw Exceptions in Java
Announcing Our New Java Error Monitoring SDK

«Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind.»

Читайте также:  Md5 class in java

Источник

What is the Cannot Find Symbol Java Error?

Java Course - Mastering the Fundamentals

The Cannot Find Symbol in Java is a compilation error that occurs when we try to refer to something that is not present in the Java Symbol Table. A very common example of this error is using a variable that is not declared in the program.

Java compilers create and maintain Symbol tables. The symbol table stores the information of the identifiers i.e. classes, interfaces, variables, and methods in the given source of code. When we use these identifiers in our code, the compiler looks up to the symbol table for information. If the identifier is not declared or is not present in the given scope the compiler throws ‘Cannot Find Symbol’.

What Causes the Java Cannot Find Symbol Error?

There are multiple ways and reasons this error can occur, they all boil down to the fact that the online Java compiler couldn’t find the identifier in the Symbol table. Some of the very commonly made mistakes are listed below:

  • Java is case sensitive, thus hello and Hello are two different variables.
  • If identifiers are misspelled say the variable’s name is name and we are trying to access nmae can cause an error.
  • Sometimes variables are declared in a particular iterating loop or method and if we try to call them from outside causes an error as it is out of the scope of the variable.
  • Using variables without their declaration is also the common cause of the Java ‘Cannot find Symbol’ error.
  • Sometimes we use methods from classes of a different package. To do so we are required to import the package of the class. Missing import statements can cause the error too.
  • Use of underscore, dollar sign, numbers, letter even a character before or after different from the initial declaration can cause ‘Cannot Find Symbol’.
Читайте также:  Java integer tostring int

Examples of Java Cannot Find Symbol error

Let us now see these common errors in the form of code:

Example 1

Let us take a very simple example of a function that takes user input of a number and returns if the number is even or odd.

In the example above, the Scanner class is not imported and thus it is throwing a Cannot Find Symbol error. The compiler is not able to find Scanner in the program. Once we import the package, it is able to identify the Scanner class.

Example 2

Another example is wrong spelling and undeclared variables. Let us take a simple example where we multiply a variable a few times in a loop.

In the example above we can see 3 errors, all of them being ‘cannot find symbol’. The first one is because N is not declared in the program, and the two are because we declared finalResult but we are calling final_Result . Thus, a single underscore can also cause an error.

Example 3

Lastly, out-of-scope variables also cause a ‘Cannot find Symbol’ error, below example demonstrates it.

In the example above, the sum variable is declared inside the for loop and its scope is only inside for loop. As we are trying to access it outside the for loop, it is causing the error.

Structure of Java Cannot Find Symbol

Look at the output generated by the compiler for the 3rd example:

output-generated-bycompiler

As we can see in the output, the compiler tells the line number of the error as well as points to the variable where the error occurred and the type of the error which is cannot find symbol in this case. The compiler also produces the cannot find symbol including these two additional fields:

  • The very first one is the symbol it is unable to find, i.e. the name and type of the referenced identifier.
  • The other field is the location of the place where the symbol is used. The class and line number are returned where the identifier has been referenced.

Cannot Find Symbol vs Symbol Not Found vs Cannot Resolve Symbol

Different compilers may use slightly different terminologies. The ‘Cannot Find Symbol’, ‘Symbol Not Found’, and ‘Cannot Resolve Symbol’ are all same errors only. Besides the naming, these convey the same meaning to the user and the compiler.

Conclusion

  • The ‘Cannot Find Symbol’ error in Java is a compilation error caused when the compiler cannot find a reference to an identifier.
  • Silly mistakes like case sensitivity, use of underscore, use of undeclared variables, use of out-of-scope variables, etc. can cause this error.
  • It is also identified by ‘Symbol Not Found’ and ‘Cannot Resolve Symbol’.

Источник

Оцените статью