What does a “Cannot find symbol” compilation error mean?
Please explain the following about the "Cannot find symbol" error:
- What does this error mean?
- What things can cause this error?
- How does the programmer go about fixing this error?
This question is designed to be a comprehensive question about "cannot find symbol" compilation errors in Java.
java compiler-errors cannot-find-symbol
add a comment |
Please explain the following about the "Cannot find symbol" error:
- What does this error mean?
- What things can cause this error?
- How does the programmer go about fixing this error?
This question is designed to be a comprehensive question about "cannot find symbol" compilation errors in Java.
java compiler-errors cannot-find-symbol
add a comment |
Please explain the following about the "Cannot find symbol" error:
- What does this error mean?
- What things can cause this error?
- How does the programmer go about fixing this error?
This question is designed to be a comprehensive question about "cannot find symbol" compilation errors in Java.
java compiler-errors cannot-find-symbol
Please explain the following about the "Cannot find symbol" error:
- What does this error mean?
- What things can cause this error?
- How does the programmer go about fixing this error?
This question is designed to be a comprehensive question about "cannot find symbol" compilation errors in Java.
java compiler-errors cannot-find-symbol
java compiler-errors cannot-find-symbol
edited Mar 22 '17 at 16:17
Taryn♦
189k46289352
189k46289352
asked Sep 7 '14 at 1:12
Stephen CStephen C
516k70566922
516k70566922
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
1. What does a "Cannot find symbol" error mean?
Firstly, it is a compilation error1. It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.
Your Java source code consists of the following things:
- Keywords: like
true
,false
,class
,while
, and so on. - Literals: like
42
and'X'
and"Hi mum!"
. - Operators and other non-alphanumeric tokens: like
+
,=
,{
, and so on. - Identifiers: like
Reader
,i
,toString
,processEquibalancedElephants
, and so on. - Comments and whitespace.
A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.
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.
2. What can cause a "Cannot find symbol" error?
As a first order, there is only one cause. The compiler looked in all of the places where the identifier should be defined, and it couldn't find the definition. This could be caused by a number of things. The common ones are as follows:
- For identifiers in general:
- Perhaps you spelled the name incorrectly; i.e.
StringBiulder
instead ofStringBuilder
. Java cannot and will not attempt to compensate for bad spelling or typing errors. - Perhaps you got the case wrong; i.e.
stringBuilder
instead ofStringBuilder
. All Java identifiers are case sensitive. - Perhaps you used underscores inappropriately; i.e.
mystring
andmy_string
are different. (If you stick to the Java style rules, you will be largely protected from this mistake ...) - Perhaps you are trying to use something that was declared "somewhere else"; i.e. in a different context to where you have implicitly told the compiler to look. (A different class? A different scope? A different package? A different code-base?)
- Perhaps you spelled the name incorrectly; i.e.
- For identifiers that should refer to variables:
- Perhaps you forgot to declare the variable.
- Perhaps the variable declaration is out of scope at the point you tried to use it. (See example below)
- For identifiers that should be method or field names:
- Perhaps you are trying to refer to an inherited method or field that wasn't declared in the parent / ancestor classes or interfaces.
- Perhaps you are trying to refer to a method or field that does not exist (i.e. has not been declared) in the type you are using; e.g.
"someString".push()
2. - Perhaps you are trying to use a method as a field, or vice versa; e.g.
"someString".length
orsomeArray.length()
.
For identifiers that should be class names:
- Perhaps you forgot to import the class.
- Perhaps you used "star" imports, but the class isn't defined in any of the packages that you imported.
Perhaps you forgot a
new
as in:
String s = String(); // should be 'new String()'
For cases where type or instance doesn't appear to have the member you were expecting it to have:
- Perhaps you have declared a nested class or a generic parameter that shadows the type you were meaning to use.
- Perhaps you are shadowing a static or instance variable.
- Perhaps you imported the wrong type; e.g. due to IDE completion or auto-correction.
- Perhaps you are using (compiling against) the wrong version of an API.
- Perhaps you forgot to cast your object to an appropriate subclass.
The problem is often a combination of the above. For example, maybe you "star" imported java.io.*
and then tried to use the Files
class ... which is in java.nio
not java.io
. Or maybe you meant to write File
... which is a class in java.io
.
Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error:
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equalsIgnoreCase("fnoord")) {
break;
}
}
if (i < strings.size()) {
...
}
This will give a "Cannot find symbol" error for i
in the if
statement. Though we previously declared i
, that declaration is only in scope for the for
statement and its body. The reference to i
in the if
statement cannot see that declaration of i
. It is out of scope.
(An appropriate correction here might be to move the if
statement inside the loop, or to declare i
before the start of the loop.)
Here is an example that causes puzzlement where a typo leads to a seemingly inexplicable "Cannot find symbol" error:
for (int i = 0; i < 100; i++); {
System.out.println("i is " + i);
}
This will give you a compilation error in the println
call saying that i
cannot be found. But (I hear you say) I did declare it!
The problem is the sneaky semicolon ( ;
) before the {
. The Java language syntax defines a semicolon in that context to be an empty statement. The empty statement then becomes the body of the for
loop. So that code actually means this:
for (int i = 0; i < 100; i++);
// The previous and following are separate statements!!
{
System.out.println("i is " + i);
}
The { ... }
block is NOT the body of the for
loop, and therefore the previous declaration of i
in the for
statement is out of scope in the block.
Here is another example of "Cannot find symbol" error that is caused by a typo.
int tmp = ...
int res = tmp(a + b);
Despite the previous declaration, the tmp
in the tmp(...)
expression is erroneous. The compiler will look for a method called tmp
, and won't find one. The previously declared tmp
is in the namespace for variables, not the namespace for methods.
In the example I came across, the programmer had actually left out an operator. What he meant to write was this:
int res = tmp * (a + b);
There is another reason why the compiler might not find a symbol if you are compiling from the command line. You might simply have forgotten to compile or recompile some other class. For example, if you have classes Foo
and Bar
where Foo
uses Bar
. If you have never compiled Bar
and you run javac Foo.java
, you are liable to find that the compiler can't find the symbol Bar
. The simple answer is to compile Foo
and Bar
together; e.g. javac Foo.java Bar.java
or javac *.java
. Or better still use a Java build tool; e.g. Ant, Maven, Gradle and so on.
There are some other more obscure causes too ... which I will deal with below.
3. How do I fix these errors ?
Generally speaking, you start out by figuring out what caused the compilation error.
- Look at the line in the file indicated by the compilation error message.
- Identify which symbol that the error message is talking about.
- Figure out why the compiler is saying that it cannot find the symbol; see above!
Then you think about what your code is supposed to be saying. Then finally you work out what correction you need to make to your source code to do what you want.
Note that not every "correction" is correct. Consider this:
for (int i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
...
}
}
Suppose that the compiler says "Cannot find symbol" for j
. There are many ways I could "fix" that:
- I could change the inner
for
tofor (int j = 1; j < 10; j++)
- probably correct. - I could add a declaration for
j
before the innerfor
loop, or the outerfor
loop - possibly correct. - I could change
j
toi
in the innerfor
loop - probably wrong! - and so on.
The point is that you need to understand what your code is trying to do in order to find the right fix.
4. Obscure causes
Here are a couple of cases where the "Cannot find symbol" is seemingly inexplicable ... until you look closer.
Incorrect dependencies: If you are using an IDE or a build tool that manages the build path and project dependencies, you may have made a mistake with the dependencies; e.g. left out a dependency, or selected the wrong version. If you are using a build tool (Ant, Maven, Gradle, etc), check the project's build file. If you are using an IDE, check the project's build path configuration.
You are not recompiling: It sometimes happens that new Java programmers don't understand how the Java tool chain works, or haven't implemented a repeatable "build process"; e.g. using an IDE, Ant, Maven, Gradle and so on. In such a situation, the programmer can end up chasing his tail looking for an illusory error that is actually caused by not recompiling the code properly, and the like ...
An earlier build problem: It is possible that an earlier build failed in a way that gave a JAR file with missing classes. Such a failure would typically be noticed if you were using a build tool. However if you are getting JAR files from someone else, you are dependent on them building properly, and noticing errors. If you suspect this, use
tar -tvf
to list the contents of the suspect JAR file.
IDE issues: People have reported cases where their IDE gets confused and the compiler in the IDE cannot find a class that exists ... or the reverse situation.
This can happen if the IDE's caches get out of sync with the file system. There are IDE specific ways to fix that.
This could be an IDE bug. For instance @Joel Costigliola describes a scenario where Eclipse does not handle a Maven "test" tree correctly: see this answer.
Redefining system classes: I've seen cases where the compiler complains that
substring
is an unknown symbol in something like the following
String s = ...
String s1 = s.substring(1);
It turned out that the programmer had created their own version of
String
and that his version of the class didn't define asubstring
methods.
Lesson: Don't define your own classes with the same names as common library classes!
Homoglyphs: If you use UTF-8 encoding for your source files, it is possible to have identifiers that look the same, but are in fact different because they contain homoglyphs. See this page for more information.
You can avoid this by restricting yourself to ASCII or Latin-1 as the source file encoding, and using Java
uxxxx
escapes for other characters.
1 - If, perchance, you do see this in a runtime exception or error message, then either you have configured your IDE to run code with compilation errors, or your application is generating and compiling code .. at runtime.
2 - The three basic principles of Civil Engineering: water doesn't flow uphill, a plank is stronger on its side, and you can't push on a string.
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
People new to Java are sometimes mixing up array types with their component types, for instanceString strings = { "hello", "world" }; strings.chatAt(3);
.
– MC Emperor
Aug 13 '18 at 11:14
add a comment |
You'll also get this error if you forget a new
:
String s = String();
versus
String s = new String();
add a comment |
One more example of 'Variable is out of scope'
As I've seen that kind of questions a few times already, maybe one more example to what's illegal even if it might feel okay.
Consider this code:
if(somethingIsTrue()) {
String message = "Everything is fine";
} else {
String message = "We have an error";
}
System.out.println(message);
That's invalid code. Because neither of the variables named message
is visible outside of their respective scope - which would be the surrounding brackets {}
in this case.
You might say: "But a variable named message is defined either way - so message is defined after the if
".
But you'd be wrong.
Java has no free()
or delete
operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause).
It's especially bad if you thought you did something good. I've seen this kind of error after "optimizing" code like this:
if(somethingIsTrue()) {
String message = "Everything is fine";
System.out.println(message);
} else {
String message = "We have an error";
System.out.println(message);
}
"Oh, there's duplicated code, let's pull that common line out" -> and there it it.
The most common way to deal with this kind of scope-trouble would be to pre-assign the else-values to the variable names in the outside scope and then reassign in if:
String message = "We have an error";
if(somethingIsTrue()) {
message = "Everything is fine";
}
System.out.println(message);
3
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
The better solution for code that assigns a different value on every branch is to use a blankfinal
variable declaration.
– Daniel Pryden
Apr 3 '18 at 13:43
add a comment |
One way to get this error in Eclipse :
- Define a class
A
insrc/test/java
. - Define another class
B
insrc/main/java
that uses classA
.
Result : Eclipse will compile the code, but maven will give "Cannot find symbol".
Underlying cause : Eclipse is using a combined build path for the main and test trees. Unfortunately, it does not support using different build paths for different parts of an Eclipse project, which is what Maven requires.
Solution :
- Don't define your dependencies that way; i.e. don't make this mistake.
- Regularly build your codebase using Maven so that you pick up this mistake early. One way to do that is to use a CI server.
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
2
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
add a comment |
"Can not find " means that , compiler who can't find appropriate variable, method ,class etc...if you got that error massage , first of all you want to find code line where get error massage..And then you will able to find which variable , method or class have not define before using it.After confirmation initialize that variable ,method or class can be used for later require...Consider the following example.
I'll create a demo class and print a name...
class demo{
public static void main(String a){
System.out.print(name);
}
}
Now look at the result..
That error says, "variable name can not find"..Defining and initializing value for 'name' variable can be abolished that error..Actually like this,
class demo{
public static void main(String a){
String name="smith";
System.out.print(name);
}
}
Now look at the new output...
Ok Successfully solved that error..At the same time , if you could get "can not find method " or "can not find class" something , At first,define a class or method and after use that..
add a comment |
If you're getting this error in the build somewhere else, while your IDE says everything is perfectly fine, then check that you are using the same Java versions in both places.
For example, Java 7 and Java 8 have different APIs, so calling a non-existent API in an older Java version would cause this error.
add a comment |
I too was getting this error. (for which I googled and I was directed to this page)
Problem: I was calling a static method defined in the class of a project A from a class defined in another project B.
I was getting the following error:
error: cannot find symbol
Solution: I resolved this by first building the project where the method is defined then the project where the method was being called from.
add a comment |
For hints, look closer at the class name name that throws an error and the line number, example:
Compilation failure
[ERROR] applicationsxxxxx.java:[44,30] error: cannot find symbol
One other cause is unsupported method of for java version say jdk7 vs 8.
Check your %JAVA_HOME%
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
add a comment |
There can be various scenarios as people have mentioned above. A couple of things which have helped me resolve this.
If you are using IntelliJ
File -> 'Invalidate Caches/Restart'
OR
The class being referenced was in another project and that dependency was not added to the Gradle build file of my project. So I added the dependency using
compile project(':anotherProject')
and it worked. HTH!
add a comment |
If eclipse Java build path is mapped to 7, 8 and in Project pom.xml Maven properties java.version is mentioned higher Java version(9,10,11, etc..,) than 7,8 you need to update in pom.xml file.
In Eclipse if Java is mapped to Java version 11 and in pom.xml it is mapped to Java version 8. Update Eclipse support to Java 11 by go through below steps in eclipse IDE
Help -> Install New Software ->
Paste following link http://download.eclipse.org/eclipse/updates/4.9-P-builds at Work With
or
Add (Popup window will open) ->
Name:
Java 11 support
Location:
http://download.eclipse.org/eclipse/updates/4.9-P-builds
then update Java version in Maven properties of pom.xml file as below
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Finally do right click on project Debug as -> Maven clean, Maven build steps
add a comment |
You can also get this error if you're forgot a new as in:
int i = Integer(); ---->> should be "new Integer()"
(This is why I get the error in my case)
Do you mean that declaring a variableBoolean b;
instead ofboolean b;
can cause this error? And to which typestring
are you referring to?
– LuCio
Aug 30 '18 at 14:42
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
This is misleading. Simply usingBoolean
instead ofboolean
orInteger
instead ofint
will not cause this error. You need to do something else as well. BothBoolean
andInteger
are valid type names, and will be understood by the Java compiler.
– Stephen C
Oct 31 '18 at 1:39
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
add a comment |
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
1. What does a "Cannot find symbol" error mean?
Firstly, it is a compilation error1. It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.
Your Java source code consists of the following things:
- Keywords: like
true
,false
,class
,while
, and so on. - Literals: like
42
and'X'
and"Hi mum!"
. - Operators and other non-alphanumeric tokens: like
+
,=
,{
, and so on. - Identifiers: like
Reader
,i
,toString
,processEquibalancedElephants
, and so on. - Comments and whitespace.
A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.
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.
2. What can cause a "Cannot find symbol" error?
As a first order, there is only one cause. The compiler looked in all of the places where the identifier should be defined, and it couldn't find the definition. This could be caused by a number of things. The common ones are as follows:
- For identifiers in general:
- Perhaps you spelled the name incorrectly; i.e.
StringBiulder
instead ofStringBuilder
. Java cannot and will not attempt to compensate for bad spelling or typing errors. - Perhaps you got the case wrong; i.e.
stringBuilder
instead ofStringBuilder
. All Java identifiers are case sensitive. - Perhaps you used underscores inappropriately; i.e.
mystring
andmy_string
are different. (If you stick to the Java style rules, you will be largely protected from this mistake ...) - Perhaps you are trying to use something that was declared "somewhere else"; i.e. in a different context to where you have implicitly told the compiler to look. (A different class? A different scope? A different package? A different code-base?)
- Perhaps you spelled the name incorrectly; i.e.
- For identifiers that should refer to variables:
- Perhaps you forgot to declare the variable.
- Perhaps the variable declaration is out of scope at the point you tried to use it. (See example below)
- For identifiers that should be method or field names:
- Perhaps you are trying to refer to an inherited method or field that wasn't declared in the parent / ancestor classes or interfaces.
- Perhaps you are trying to refer to a method or field that does not exist (i.e. has not been declared) in the type you are using; e.g.
"someString".push()
2. - Perhaps you are trying to use a method as a field, or vice versa; e.g.
"someString".length
orsomeArray.length()
.
For identifiers that should be class names:
- Perhaps you forgot to import the class.
- Perhaps you used "star" imports, but the class isn't defined in any of the packages that you imported.
Perhaps you forgot a
new
as in:
String s = String(); // should be 'new String()'
For cases where type or instance doesn't appear to have the member you were expecting it to have:
- Perhaps you have declared a nested class or a generic parameter that shadows the type you were meaning to use.
- Perhaps you are shadowing a static or instance variable.
- Perhaps you imported the wrong type; e.g. due to IDE completion or auto-correction.
- Perhaps you are using (compiling against) the wrong version of an API.
- Perhaps you forgot to cast your object to an appropriate subclass.
The problem is often a combination of the above. For example, maybe you "star" imported java.io.*
and then tried to use the Files
class ... which is in java.nio
not java.io
. Or maybe you meant to write File
... which is a class in java.io
.
Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error:
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equalsIgnoreCase("fnoord")) {
break;
}
}
if (i < strings.size()) {
...
}
This will give a "Cannot find symbol" error for i
in the if
statement. Though we previously declared i
, that declaration is only in scope for the for
statement and its body. The reference to i
in the if
statement cannot see that declaration of i
. It is out of scope.
(An appropriate correction here might be to move the if
statement inside the loop, or to declare i
before the start of the loop.)
Here is an example that causes puzzlement where a typo leads to a seemingly inexplicable "Cannot find symbol" error:
for (int i = 0; i < 100; i++); {
System.out.println("i is " + i);
}
This will give you a compilation error in the println
call saying that i
cannot be found. But (I hear you say) I did declare it!
The problem is the sneaky semicolon ( ;
) before the {
. The Java language syntax defines a semicolon in that context to be an empty statement. The empty statement then becomes the body of the for
loop. So that code actually means this:
for (int i = 0; i < 100; i++);
// The previous and following are separate statements!!
{
System.out.println("i is " + i);
}
The { ... }
block is NOT the body of the for
loop, and therefore the previous declaration of i
in the for
statement is out of scope in the block.
Here is another example of "Cannot find symbol" error that is caused by a typo.
int tmp = ...
int res = tmp(a + b);
Despite the previous declaration, the tmp
in the tmp(...)
expression is erroneous. The compiler will look for a method called tmp
, and won't find one. The previously declared tmp
is in the namespace for variables, not the namespace for methods.
In the example I came across, the programmer had actually left out an operator. What he meant to write was this:
int res = tmp * (a + b);
There is another reason why the compiler might not find a symbol if you are compiling from the command line. You might simply have forgotten to compile or recompile some other class. For example, if you have classes Foo
and Bar
where Foo
uses Bar
. If you have never compiled Bar
and you run javac Foo.java
, you are liable to find that the compiler can't find the symbol Bar
. The simple answer is to compile Foo
and Bar
together; e.g. javac Foo.java Bar.java
or javac *.java
. Or better still use a Java build tool; e.g. Ant, Maven, Gradle and so on.
There are some other more obscure causes too ... which I will deal with below.
3. How do I fix these errors ?
Generally speaking, you start out by figuring out what caused the compilation error.
- Look at the line in the file indicated by the compilation error message.
- Identify which symbol that the error message is talking about.
- Figure out why the compiler is saying that it cannot find the symbol; see above!
Then you think about what your code is supposed to be saying. Then finally you work out what correction you need to make to your source code to do what you want.
Note that not every "correction" is correct. Consider this:
for (int i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
...
}
}
Suppose that the compiler says "Cannot find symbol" for j
. There are many ways I could "fix" that:
- I could change the inner
for
tofor (int j = 1; j < 10; j++)
- probably correct. - I could add a declaration for
j
before the innerfor
loop, or the outerfor
loop - possibly correct. - I could change
j
toi
in the innerfor
loop - probably wrong! - and so on.
The point is that you need to understand what your code is trying to do in order to find the right fix.
4. Obscure causes
Here are a couple of cases where the "Cannot find symbol" is seemingly inexplicable ... until you look closer.
Incorrect dependencies: If you are using an IDE or a build tool that manages the build path and project dependencies, you may have made a mistake with the dependencies; e.g. left out a dependency, or selected the wrong version. If you are using a build tool (Ant, Maven, Gradle, etc), check the project's build file. If you are using an IDE, check the project's build path configuration.
You are not recompiling: It sometimes happens that new Java programmers don't understand how the Java tool chain works, or haven't implemented a repeatable "build process"; e.g. using an IDE, Ant, Maven, Gradle and so on. In such a situation, the programmer can end up chasing his tail looking for an illusory error that is actually caused by not recompiling the code properly, and the like ...
An earlier build problem: It is possible that an earlier build failed in a way that gave a JAR file with missing classes. Such a failure would typically be noticed if you were using a build tool. However if you are getting JAR files from someone else, you are dependent on them building properly, and noticing errors. If you suspect this, use
tar -tvf
to list the contents of the suspect JAR file.
IDE issues: People have reported cases where their IDE gets confused and the compiler in the IDE cannot find a class that exists ... or the reverse situation.
This can happen if the IDE's caches get out of sync with the file system. There are IDE specific ways to fix that.
This could be an IDE bug. For instance @Joel Costigliola describes a scenario where Eclipse does not handle a Maven "test" tree correctly: see this answer.
Redefining system classes: I've seen cases where the compiler complains that
substring
is an unknown symbol in something like the following
String s = ...
String s1 = s.substring(1);
It turned out that the programmer had created their own version of
String
and that his version of the class didn't define asubstring
methods.
Lesson: Don't define your own classes with the same names as common library classes!
Homoglyphs: If you use UTF-8 encoding for your source files, it is possible to have identifiers that look the same, but are in fact different because they contain homoglyphs. See this page for more information.
You can avoid this by restricting yourself to ASCII or Latin-1 as the source file encoding, and using Java
uxxxx
escapes for other characters.
1 - If, perchance, you do see this in a runtime exception or error message, then either you have configured your IDE to run code with compilation errors, or your application is generating and compiling code .. at runtime.
2 - The three basic principles of Civil Engineering: water doesn't flow uphill, a plank is stronger on its side, and you can't push on a string.
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
People new to Java are sometimes mixing up array types with their component types, for instanceString strings = { "hello", "world" }; strings.chatAt(3);
.
– MC Emperor
Aug 13 '18 at 11:14
add a comment |
1. What does a "Cannot find symbol" error mean?
Firstly, it is a compilation error1. It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.
Your Java source code consists of the following things:
- Keywords: like
true
,false
,class
,while
, and so on. - Literals: like
42
and'X'
and"Hi mum!"
. - Operators and other non-alphanumeric tokens: like
+
,=
,{
, and so on. - Identifiers: like
Reader
,i
,toString
,processEquibalancedElephants
, and so on. - Comments and whitespace.
A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.
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.
2. What can cause a "Cannot find symbol" error?
As a first order, there is only one cause. The compiler looked in all of the places where the identifier should be defined, and it couldn't find the definition. This could be caused by a number of things. The common ones are as follows:
- For identifiers in general:
- Perhaps you spelled the name incorrectly; i.e.
StringBiulder
instead ofStringBuilder
. Java cannot and will not attempt to compensate for bad spelling or typing errors. - Perhaps you got the case wrong; i.e.
stringBuilder
instead ofStringBuilder
. All Java identifiers are case sensitive. - Perhaps you used underscores inappropriately; i.e.
mystring
andmy_string
are different. (If you stick to the Java style rules, you will be largely protected from this mistake ...) - Perhaps you are trying to use something that was declared "somewhere else"; i.e. in a different context to where you have implicitly told the compiler to look. (A different class? A different scope? A different package? A different code-base?)
- Perhaps you spelled the name incorrectly; i.e.
- For identifiers that should refer to variables:
- Perhaps you forgot to declare the variable.
- Perhaps the variable declaration is out of scope at the point you tried to use it. (See example below)
- For identifiers that should be method or field names:
- Perhaps you are trying to refer to an inherited method or field that wasn't declared in the parent / ancestor classes or interfaces.
- Perhaps you are trying to refer to a method or field that does not exist (i.e. has not been declared) in the type you are using; e.g.
"someString".push()
2. - Perhaps you are trying to use a method as a field, or vice versa; e.g.
"someString".length
orsomeArray.length()
.
For identifiers that should be class names:
- Perhaps you forgot to import the class.
- Perhaps you used "star" imports, but the class isn't defined in any of the packages that you imported.
Perhaps you forgot a
new
as in:
String s = String(); // should be 'new String()'
For cases where type or instance doesn't appear to have the member you were expecting it to have:
- Perhaps you have declared a nested class or a generic parameter that shadows the type you were meaning to use.
- Perhaps you are shadowing a static or instance variable.
- Perhaps you imported the wrong type; e.g. due to IDE completion or auto-correction.
- Perhaps you are using (compiling against) the wrong version of an API.
- Perhaps you forgot to cast your object to an appropriate subclass.
The problem is often a combination of the above. For example, maybe you "star" imported java.io.*
and then tried to use the Files
class ... which is in java.nio
not java.io
. Or maybe you meant to write File
... which is a class in java.io
.
Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error:
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equalsIgnoreCase("fnoord")) {
break;
}
}
if (i < strings.size()) {
...
}
This will give a "Cannot find symbol" error for i
in the if
statement. Though we previously declared i
, that declaration is only in scope for the for
statement and its body. The reference to i
in the if
statement cannot see that declaration of i
. It is out of scope.
(An appropriate correction here might be to move the if
statement inside the loop, or to declare i
before the start of the loop.)
Here is an example that causes puzzlement where a typo leads to a seemingly inexplicable "Cannot find symbol" error:
for (int i = 0; i < 100; i++); {
System.out.println("i is " + i);
}
This will give you a compilation error in the println
call saying that i
cannot be found. But (I hear you say) I did declare it!
The problem is the sneaky semicolon ( ;
) before the {
. The Java language syntax defines a semicolon in that context to be an empty statement. The empty statement then becomes the body of the for
loop. So that code actually means this:
for (int i = 0; i < 100; i++);
// The previous and following are separate statements!!
{
System.out.println("i is " + i);
}
The { ... }
block is NOT the body of the for
loop, and therefore the previous declaration of i
in the for
statement is out of scope in the block.
Here is another example of "Cannot find symbol" error that is caused by a typo.
int tmp = ...
int res = tmp(a + b);
Despite the previous declaration, the tmp
in the tmp(...)
expression is erroneous. The compiler will look for a method called tmp
, and won't find one. The previously declared tmp
is in the namespace for variables, not the namespace for methods.
In the example I came across, the programmer had actually left out an operator. What he meant to write was this:
int res = tmp * (a + b);
There is another reason why the compiler might not find a symbol if you are compiling from the command line. You might simply have forgotten to compile or recompile some other class. For example, if you have classes Foo
and Bar
where Foo
uses Bar
. If you have never compiled Bar
and you run javac Foo.java
, you are liable to find that the compiler can't find the symbol Bar
. The simple answer is to compile Foo
and Bar
together; e.g. javac Foo.java Bar.java
or javac *.java
. Or better still use a Java build tool; e.g. Ant, Maven, Gradle and so on.
There are some other more obscure causes too ... which I will deal with below.
3. How do I fix these errors ?
Generally speaking, you start out by figuring out what caused the compilation error.
- Look at the line in the file indicated by the compilation error message.
- Identify which symbol that the error message is talking about.
- Figure out why the compiler is saying that it cannot find the symbol; see above!
Then you think about what your code is supposed to be saying. Then finally you work out what correction you need to make to your source code to do what you want.
Note that not every "correction" is correct. Consider this:
for (int i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
...
}
}
Suppose that the compiler says "Cannot find symbol" for j
. There are many ways I could "fix" that:
- I could change the inner
for
tofor (int j = 1; j < 10; j++)
- probably correct. - I could add a declaration for
j
before the innerfor
loop, or the outerfor
loop - possibly correct. - I could change
j
toi
in the innerfor
loop - probably wrong! - and so on.
The point is that you need to understand what your code is trying to do in order to find the right fix.
4. Obscure causes
Here are a couple of cases where the "Cannot find symbol" is seemingly inexplicable ... until you look closer.
Incorrect dependencies: If you are using an IDE or a build tool that manages the build path and project dependencies, you may have made a mistake with the dependencies; e.g. left out a dependency, or selected the wrong version. If you are using a build tool (Ant, Maven, Gradle, etc), check the project's build file. If you are using an IDE, check the project's build path configuration.
You are not recompiling: It sometimes happens that new Java programmers don't understand how the Java tool chain works, or haven't implemented a repeatable "build process"; e.g. using an IDE, Ant, Maven, Gradle and so on. In such a situation, the programmer can end up chasing his tail looking for an illusory error that is actually caused by not recompiling the code properly, and the like ...
An earlier build problem: It is possible that an earlier build failed in a way that gave a JAR file with missing classes. Such a failure would typically be noticed if you were using a build tool. However if you are getting JAR files from someone else, you are dependent on them building properly, and noticing errors. If you suspect this, use
tar -tvf
to list the contents of the suspect JAR file.
IDE issues: People have reported cases where their IDE gets confused and the compiler in the IDE cannot find a class that exists ... or the reverse situation.
This can happen if the IDE's caches get out of sync with the file system. There are IDE specific ways to fix that.
This could be an IDE bug. For instance @Joel Costigliola describes a scenario where Eclipse does not handle a Maven "test" tree correctly: see this answer.
Redefining system classes: I've seen cases where the compiler complains that
substring
is an unknown symbol in something like the following
String s = ...
String s1 = s.substring(1);
It turned out that the programmer had created their own version of
String
and that his version of the class didn't define asubstring
methods.
Lesson: Don't define your own classes with the same names as common library classes!
Homoglyphs: If you use UTF-8 encoding for your source files, it is possible to have identifiers that look the same, but are in fact different because they contain homoglyphs. See this page for more information.
You can avoid this by restricting yourself to ASCII or Latin-1 as the source file encoding, and using Java
uxxxx
escapes for other characters.
1 - If, perchance, you do see this in a runtime exception or error message, then either you have configured your IDE to run code with compilation errors, or your application is generating and compiling code .. at runtime.
2 - The three basic principles of Civil Engineering: water doesn't flow uphill, a plank is stronger on its side, and you can't push on a string.
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
People new to Java are sometimes mixing up array types with their component types, for instanceString strings = { "hello", "world" }; strings.chatAt(3);
.
– MC Emperor
Aug 13 '18 at 11:14
add a comment |
1. What does a "Cannot find symbol" error mean?
Firstly, it is a compilation error1. It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.
Your Java source code consists of the following things:
- Keywords: like
true
,false
,class
,while
, and so on. - Literals: like
42
and'X'
and"Hi mum!"
. - Operators and other non-alphanumeric tokens: like
+
,=
,{
, and so on. - Identifiers: like
Reader
,i
,toString
,processEquibalancedElephants
, and so on. - Comments and whitespace.
A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.
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.
2. What can cause a "Cannot find symbol" error?
As a first order, there is only one cause. The compiler looked in all of the places where the identifier should be defined, and it couldn't find the definition. This could be caused by a number of things. The common ones are as follows:
- For identifiers in general:
- Perhaps you spelled the name incorrectly; i.e.
StringBiulder
instead ofStringBuilder
. Java cannot and will not attempt to compensate for bad spelling or typing errors. - Perhaps you got the case wrong; i.e.
stringBuilder
instead ofStringBuilder
. All Java identifiers are case sensitive. - Perhaps you used underscores inappropriately; i.e.
mystring
andmy_string
are different. (If you stick to the Java style rules, you will be largely protected from this mistake ...) - Perhaps you are trying to use something that was declared "somewhere else"; i.e. in a different context to where you have implicitly told the compiler to look. (A different class? A different scope? A different package? A different code-base?)
- Perhaps you spelled the name incorrectly; i.e.
- For identifiers that should refer to variables:
- Perhaps you forgot to declare the variable.
- Perhaps the variable declaration is out of scope at the point you tried to use it. (See example below)
- For identifiers that should be method or field names:
- Perhaps you are trying to refer to an inherited method or field that wasn't declared in the parent / ancestor classes or interfaces.
- Perhaps you are trying to refer to a method or field that does not exist (i.e. has not been declared) in the type you are using; e.g.
"someString".push()
2. - Perhaps you are trying to use a method as a field, or vice versa; e.g.
"someString".length
orsomeArray.length()
.
For identifiers that should be class names:
- Perhaps you forgot to import the class.
- Perhaps you used "star" imports, but the class isn't defined in any of the packages that you imported.
Perhaps you forgot a
new
as in:
String s = String(); // should be 'new String()'
For cases where type or instance doesn't appear to have the member you were expecting it to have:
- Perhaps you have declared a nested class or a generic parameter that shadows the type you were meaning to use.
- Perhaps you are shadowing a static or instance variable.
- Perhaps you imported the wrong type; e.g. due to IDE completion or auto-correction.
- Perhaps you are using (compiling against) the wrong version of an API.
- Perhaps you forgot to cast your object to an appropriate subclass.
The problem is often a combination of the above. For example, maybe you "star" imported java.io.*
and then tried to use the Files
class ... which is in java.nio
not java.io
. Or maybe you meant to write File
... which is a class in java.io
.
Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error:
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equalsIgnoreCase("fnoord")) {
break;
}
}
if (i < strings.size()) {
...
}
This will give a "Cannot find symbol" error for i
in the if
statement. Though we previously declared i
, that declaration is only in scope for the for
statement and its body. The reference to i
in the if
statement cannot see that declaration of i
. It is out of scope.
(An appropriate correction here might be to move the if
statement inside the loop, or to declare i
before the start of the loop.)
Here is an example that causes puzzlement where a typo leads to a seemingly inexplicable "Cannot find symbol" error:
for (int i = 0; i < 100; i++); {
System.out.println("i is " + i);
}
This will give you a compilation error in the println
call saying that i
cannot be found. But (I hear you say) I did declare it!
The problem is the sneaky semicolon ( ;
) before the {
. The Java language syntax defines a semicolon in that context to be an empty statement. The empty statement then becomes the body of the for
loop. So that code actually means this:
for (int i = 0; i < 100; i++);
// The previous and following are separate statements!!
{
System.out.println("i is " + i);
}
The { ... }
block is NOT the body of the for
loop, and therefore the previous declaration of i
in the for
statement is out of scope in the block.
Here is another example of "Cannot find symbol" error that is caused by a typo.
int tmp = ...
int res = tmp(a + b);
Despite the previous declaration, the tmp
in the tmp(...)
expression is erroneous. The compiler will look for a method called tmp
, and won't find one. The previously declared tmp
is in the namespace for variables, not the namespace for methods.
In the example I came across, the programmer had actually left out an operator. What he meant to write was this:
int res = tmp * (a + b);
There is another reason why the compiler might not find a symbol if you are compiling from the command line. You might simply have forgotten to compile or recompile some other class. For example, if you have classes Foo
and Bar
where Foo
uses Bar
. If you have never compiled Bar
and you run javac Foo.java
, you are liable to find that the compiler can't find the symbol Bar
. The simple answer is to compile Foo
and Bar
together; e.g. javac Foo.java Bar.java
or javac *.java
. Or better still use a Java build tool; e.g. Ant, Maven, Gradle and so on.
There are some other more obscure causes too ... which I will deal with below.
3. How do I fix these errors ?
Generally speaking, you start out by figuring out what caused the compilation error.
- Look at the line in the file indicated by the compilation error message.
- Identify which symbol that the error message is talking about.
- Figure out why the compiler is saying that it cannot find the symbol; see above!
Then you think about what your code is supposed to be saying. Then finally you work out what correction you need to make to your source code to do what you want.
Note that not every "correction" is correct. Consider this:
for (int i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
...
}
}
Suppose that the compiler says "Cannot find symbol" for j
. There are many ways I could "fix" that:
- I could change the inner
for
tofor (int j = 1; j < 10; j++)
- probably correct. - I could add a declaration for
j
before the innerfor
loop, or the outerfor
loop - possibly correct. - I could change
j
toi
in the innerfor
loop - probably wrong! - and so on.
The point is that you need to understand what your code is trying to do in order to find the right fix.
4. Obscure causes
Here are a couple of cases where the "Cannot find symbol" is seemingly inexplicable ... until you look closer.
Incorrect dependencies: If you are using an IDE or a build tool that manages the build path and project dependencies, you may have made a mistake with the dependencies; e.g. left out a dependency, or selected the wrong version. If you are using a build tool (Ant, Maven, Gradle, etc), check the project's build file. If you are using an IDE, check the project's build path configuration.
You are not recompiling: It sometimes happens that new Java programmers don't understand how the Java tool chain works, or haven't implemented a repeatable "build process"; e.g. using an IDE, Ant, Maven, Gradle and so on. In such a situation, the programmer can end up chasing his tail looking for an illusory error that is actually caused by not recompiling the code properly, and the like ...
An earlier build problem: It is possible that an earlier build failed in a way that gave a JAR file with missing classes. Such a failure would typically be noticed if you were using a build tool. However if you are getting JAR files from someone else, you are dependent on them building properly, and noticing errors. If you suspect this, use
tar -tvf
to list the contents of the suspect JAR file.
IDE issues: People have reported cases where their IDE gets confused and the compiler in the IDE cannot find a class that exists ... or the reverse situation.
This can happen if the IDE's caches get out of sync with the file system. There are IDE specific ways to fix that.
This could be an IDE bug. For instance @Joel Costigliola describes a scenario where Eclipse does not handle a Maven "test" tree correctly: see this answer.
Redefining system classes: I've seen cases where the compiler complains that
substring
is an unknown symbol in something like the following
String s = ...
String s1 = s.substring(1);
It turned out that the programmer had created their own version of
String
and that his version of the class didn't define asubstring
methods.
Lesson: Don't define your own classes with the same names as common library classes!
Homoglyphs: If you use UTF-8 encoding for your source files, it is possible to have identifiers that look the same, but are in fact different because they contain homoglyphs. See this page for more information.
You can avoid this by restricting yourself to ASCII or Latin-1 as the source file encoding, and using Java
uxxxx
escapes for other characters.
1 - If, perchance, you do see this in a runtime exception or error message, then either you have configured your IDE to run code with compilation errors, or your application is generating and compiling code .. at runtime.
2 - The three basic principles of Civil Engineering: water doesn't flow uphill, a plank is stronger on its side, and you can't push on a string.
1. What does a "Cannot find symbol" error mean?
Firstly, it is a compilation error1. It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.
Your Java source code consists of the following things:
- Keywords: like
true
,false
,class
,while
, and so on. - Literals: like
42
and'X'
and"Hi mum!"
. - Operators and other non-alphanumeric tokens: like
+
,=
,{
, and so on. - Identifiers: like
Reader
,i
,toString
,processEquibalancedElephants
, and so on. - Comments and whitespace.
A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.
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.
2. What can cause a "Cannot find symbol" error?
As a first order, there is only one cause. The compiler looked in all of the places where the identifier should be defined, and it couldn't find the definition. This could be caused by a number of things. The common ones are as follows:
- For identifiers in general:
- Perhaps you spelled the name incorrectly; i.e.
StringBiulder
instead ofStringBuilder
. Java cannot and will not attempt to compensate for bad spelling or typing errors. - Perhaps you got the case wrong; i.e.
stringBuilder
instead ofStringBuilder
. All Java identifiers are case sensitive. - Perhaps you used underscores inappropriately; i.e.
mystring
andmy_string
are different. (If you stick to the Java style rules, you will be largely protected from this mistake ...) - Perhaps you are trying to use something that was declared "somewhere else"; i.e. in a different context to where you have implicitly told the compiler to look. (A different class? A different scope? A different package? A different code-base?)
- Perhaps you spelled the name incorrectly; i.e.
- For identifiers that should refer to variables:
- Perhaps you forgot to declare the variable.
- Perhaps the variable declaration is out of scope at the point you tried to use it. (See example below)
- For identifiers that should be method or field names:
- Perhaps you are trying to refer to an inherited method or field that wasn't declared in the parent / ancestor classes or interfaces.
- Perhaps you are trying to refer to a method or field that does not exist (i.e. has not been declared) in the type you are using; e.g.
"someString".push()
2. - Perhaps you are trying to use a method as a field, or vice versa; e.g.
"someString".length
orsomeArray.length()
.
For identifiers that should be class names:
- Perhaps you forgot to import the class.
- Perhaps you used "star" imports, but the class isn't defined in any of the packages that you imported.
Perhaps you forgot a
new
as in:
String s = String(); // should be 'new String()'
For cases where type or instance doesn't appear to have the member you were expecting it to have:
- Perhaps you have declared a nested class or a generic parameter that shadows the type you were meaning to use.
- Perhaps you are shadowing a static or instance variable.
- Perhaps you imported the wrong type; e.g. due to IDE completion or auto-correction.
- Perhaps you are using (compiling against) the wrong version of an API.
- Perhaps you forgot to cast your object to an appropriate subclass.
The problem is often a combination of the above. For example, maybe you "star" imported java.io.*
and then tried to use the Files
class ... which is in java.nio
not java.io
. Or maybe you meant to write File
... which is a class in java.io
.
Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error:
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equalsIgnoreCase("fnoord")) {
break;
}
}
if (i < strings.size()) {
...
}
This will give a "Cannot find symbol" error for i
in the if
statement. Though we previously declared i
, that declaration is only in scope for the for
statement and its body. The reference to i
in the if
statement cannot see that declaration of i
. It is out of scope.
(An appropriate correction here might be to move the if
statement inside the loop, or to declare i
before the start of the loop.)
Here is an example that causes puzzlement where a typo leads to a seemingly inexplicable "Cannot find symbol" error:
for (int i = 0; i < 100; i++); {
System.out.println("i is " + i);
}
This will give you a compilation error in the println
call saying that i
cannot be found. But (I hear you say) I did declare it!
The problem is the sneaky semicolon ( ;
) before the {
. The Java language syntax defines a semicolon in that context to be an empty statement. The empty statement then becomes the body of the for
loop. So that code actually means this:
for (int i = 0; i < 100; i++);
// The previous and following are separate statements!!
{
System.out.println("i is " + i);
}
The { ... }
block is NOT the body of the for
loop, and therefore the previous declaration of i
in the for
statement is out of scope in the block.
Here is another example of "Cannot find symbol" error that is caused by a typo.
int tmp = ...
int res = tmp(a + b);
Despite the previous declaration, the tmp
in the tmp(...)
expression is erroneous. The compiler will look for a method called tmp
, and won't find one. The previously declared tmp
is in the namespace for variables, not the namespace for methods.
In the example I came across, the programmer had actually left out an operator. What he meant to write was this:
int res = tmp * (a + b);
There is another reason why the compiler might not find a symbol if you are compiling from the command line. You might simply have forgotten to compile or recompile some other class. For example, if you have classes Foo
and Bar
where Foo
uses Bar
. If you have never compiled Bar
and you run javac Foo.java
, you are liable to find that the compiler can't find the symbol Bar
. The simple answer is to compile Foo
and Bar
together; e.g. javac Foo.java Bar.java
or javac *.java
. Or better still use a Java build tool; e.g. Ant, Maven, Gradle and so on.
There are some other more obscure causes too ... which I will deal with below.
3. How do I fix these errors ?
Generally speaking, you start out by figuring out what caused the compilation error.
- Look at the line in the file indicated by the compilation error message.
- Identify which symbol that the error message is talking about.
- Figure out why the compiler is saying that it cannot find the symbol; see above!
Then you think about what your code is supposed to be saying. Then finally you work out what correction you need to make to your source code to do what you want.
Note that not every "correction" is correct. Consider this:
for (int i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
...
}
}
Suppose that the compiler says "Cannot find symbol" for j
. There are many ways I could "fix" that:
- I could change the inner
for
tofor (int j = 1; j < 10; j++)
- probably correct. - I could add a declaration for
j
before the innerfor
loop, or the outerfor
loop - possibly correct. - I could change
j
toi
in the innerfor
loop - probably wrong! - and so on.
The point is that you need to understand what your code is trying to do in order to find the right fix.
4. Obscure causes
Here are a couple of cases where the "Cannot find symbol" is seemingly inexplicable ... until you look closer.
Incorrect dependencies: If you are using an IDE or a build tool that manages the build path and project dependencies, you may have made a mistake with the dependencies; e.g. left out a dependency, or selected the wrong version. If you are using a build tool (Ant, Maven, Gradle, etc), check the project's build file. If you are using an IDE, check the project's build path configuration.
You are not recompiling: It sometimes happens that new Java programmers don't understand how the Java tool chain works, or haven't implemented a repeatable "build process"; e.g. using an IDE, Ant, Maven, Gradle and so on. In such a situation, the programmer can end up chasing his tail looking for an illusory error that is actually caused by not recompiling the code properly, and the like ...
An earlier build problem: It is possible that an earlier build failed in a way that gave a JAR file with missing classes. Such a failure would typically be noticed if you were using a build tool. However if you are getting JAR files from someone else, you are dependent on them building properly, and noticing errors. If you suspect this, use
tar -tvf
to list the contents of the suspect JAR file.
IDE issues: People have reported cases where their IDE gets confused and the compiler in the IDE cannot find a class that exists ... or the reverse situation.
This can happen if the IDE's caches get out of sync with the file system. There are IDE specific ways to fix that.
This could be an IDE bug. For instance @Joel Costigliola describes a scenario where Eclipse does not handle a Maven "test" tree correctly: see this answer.
Redefining system classes: I've seen cases where the compiler complains that
substring
is an unknown symbol in something like the following
String s = ...
String s1 = s.substring(1);
It turned out that the programmer had created their own version of
String
and that his version of the class didn't define asubstring
methods.
Lesson: Don't define your own classes with the same names as common library classes!
Homoglyphs: If you use UTF-8 encoding for your source files, it is possible to have identifiers that look the same, but are in fact different because they contain homoglyphs. See this page for more information.
You can avoid this by restricting yourself to ASCII or Latin-1 as the source file encoding, and using Java
uxxxx
escapes for other characters.
1 - If, perchance, you do see this in a runtime exception or error message, then either you have configured your IDE to run code with compilation errors, or your application is generating and compiling code .. at runtime.
2 - The three basic principles of Civil Engineering: water doesn't flow uphill, a plank is stronger on its side, and you can't push on a string.
edited Oct 31 '18 at 1:51
answered Sep 7 '14 at 1:12
Stephen CStephen C
516k70566922
516k70566922
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
People new to Java are sometimes mixing up array types with their component types, for instanceString strings = { "hello", "world" }; strings.chatAt(3);
.
– MC Emperor
Aug 13 '18 at 11:14
add a comment |
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
People new to Java are sometimes mixing up array types with their component types, for instanceString strings = { "hello", "world" }; strings.chatAt(3);
.
– MC Emperor
Aug 13 '18 at 11:14
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
I had another situation where this compilation error occured while eclipse didn't see the problem: Two classes with dependencies defined in the respectively other class. In my case I had an enum, implementing an interface, defined in a class where I foolishly already used the enum.
– Jogi
May 23 '16 at 7:28
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
Somewhat similarly to the comment above, when I compile and run my program from Eclipse it works no problem. Compiling it from the console raises a bunch of these "Cannot find symbol" errors often related to last element in an import. I have no idea what is causing this as there is nothing wrong in the code really.
– Andres Stadelmann
May 27 '16 at 16:42
People new to Java are sometimes mixing up array types with their component types, for instance
String strings = { "hello", "world" }; strings.chatAt(3);
.– MC Emperor
Aug 13 '18 at 11:14
People new to Java are sometimes mixing up array types with their component types, for instance
String strings = { "hello", "world" }; strings.chatAt(3);
.– MC Emperor
Aug 13 '18 at 11:14
add a comment |
You'll also get this error if you forget a new
:
String s = String();
versus
String s = new String();
add a comment |
You'll also get this error if you forget a new
:
String s = String();
versus
String s = new String();
add a comment |
You'll also get this error if you forget a new
:
String s = String();
versus
String s = new String();
You'll also get this error if you forget a new
:
String s = String();
versus
String s = new String();
answered Apr 17 '15 at 8:50
thinkterrythinkterry
3951410
3951410
add a comment |
add a comment |
One more example of 'Variable is out of scope'
As I've seen that kind of questions a few times already, maybe one more example to what's illegal even if it might feel okay.
Consider this code:
if(somethingIsTrue()) {
String message = "Everything is fine";
} else {
String message = "We have an error";
}
System.out.println(message);
That's invalid code. Because neither of the variables named message
is visible outside of their respective scope - which would be the surrounding brackets {}
in this case.
You might say: "But a variable named message is defined either way - so message is defined after the if
".
But you'd be wrong.
Java has no free()
or delete
operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause).
It's especially bad if you thought you did something good. I've seen this kind of error after "optimizing" code like this:
if(somethingIsTrue()) {
String message = "Everything is fine";
System.out.println(message);
} else {
String message = "We have an error";
System.out.println(message);
}
"Oh, there's duplicated code, let's pull that common line out" -> and there it it.
The most common way to deal with this kind of scope-trouble would be to pre-assign the else-values to the variable names in the outside scope and then reassign in if:
String message = "We have an error";
if(somethingIsTrue()) {
message = "Everything is fine";
}
System.out.println(message);
3
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
The better solution for code that assigns a different value on every branch is to use a blankfinal
variable declaration.
– Daniel Pryden
Apr 3 '18 at 13:43
add a comment |
One more example of 'Variable is out of scope'
As I've seen that kind of questions a few times already, maybe one more example to what's illegal even if it might feel okay.
Consider this code:
if(somethingIsTrue()) {
String message = "Everything is fine";
} else {
String message = "We have an error";
}
System.out.println(message);
That's invalid code. Because neither of the variables named message
is visible outside of their respective scope - which would be the surrounding brackets {}
in this case.
You might say: "But a variable named message is defined either way - so message is defined after the if
".
But you'd be wrong.
Java has no free()
or delete
operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause).
It's especially bad if you thought you did something good. I've seen this kind of error after "optimizing" code like this:
if(somethingIsTrue()) {
String message = "Everything is fine";
System.out.println(message);
} else {
String message = "We have an error";
System.out.println(message);
}
"Oh, there's duplicated code, let's pull that common line out" -> and there it it.
The most common way to deal with this kind of scope-trouble would be to pre-assign the else-values to the variable names in the outside scope and then reassign in if:
String message = "We have an error";
if(somethingIsTrue()) {
message = "Everything is fine";
}
System.out.println(message);
3
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
The better solution for code that assigns a different value on every branch is to use a blankfinal
variable declaration.
– Daniel Pryden
Apr 3 '18 at 13:43
add a comment |
One more example of 'Variable is out of scope'
As I've seen that kind of questions a few times already, maybe one more example to what's illegal even if it might feel okay.
Consider this code:
if(somethingIsTrue()) {
String message = "Everything is fine";
} else {
String message = "We have an error";
}
System.out.println(message);
That's invalid code. Because neither of the variables named message
is visible outside of their respective scope - which would be the surrounding brackets {}
in this case.
You might say: "But a variable named message is defined either way - so message is defined after the if
".
But you'd be wrong.
Java has no free()
or delete
operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause).
It's especially bad if you thought you did something good. I've seen this kind of error after "optimizing" code like this:
if(somethingIsTrue()) {
String message = "Everything is fine";
System.out.println(message);
} else {
String message = "We have an error";
System.out.println(message);
}
"Oh, there's duplicated code, let's pull that common line out" -> and there it it.
The most common way to deal with this kind of scope-trouble would be to pre-assign the else-values to the variable names in the outside scope and then reassign in if:
String message = "We have an error";
if(somethingIsTrue()) {
message = "Everything is fine";
}
System.out.println(message);
One more example of 'Variable is out of scope'
As I've seen that kind of questions a few times already, maybe one more example to what's illegal even if it might feel okay.
Consider this code:
if(somethingIsTrue()) {
String message = "Everything is fine";
} else {
String message = "We have an error";
}
System.out.println(message);
That's invalid code. Because neither of the variables named message
is visible outside of their respective scope - which would be the surrounding brackets {}
in this case.
You might say: "But a variable named message is defined either way - so message is defined after the if
".
But you'd be wrong.
Java has no free()
or delete
operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause).
It's especially bad if you thought you did something good. I've seen this kind of error after "optimizing" code like this:
if(somethingIsTrue()) {
String message = "Everything is fine";
System.out.println(message);
} else {
String message = "We have an error";
System.out.println(message);
}
"Oh, there's duplicated code, let's pull that common line out" -> and there it it.
The most common way to deal with this kind of scope-trouble would be to pre-assign the else-values to the variable names in the outside scope and then reassign in if:
String message = "We have an error";
if(somethingIsTrue()) {
message = "Everything is fine";
}
System.out.println(message);
edited Nov 1 '16 at 3:42
Ken Y-N
7,734134571
7,734134571
answered Dec 6 '15 at 9:19
JanJan
12.4k32043
12.4k32043
3
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
The better solution for code that assigns a different value on every branch is to use a blankfinal
variable declaration.
– Daniel Pryden
Apr 3 '18 at 13:43
add a comment |
3
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
The better solution for code that assigns a different value on every branch is to use a blankfinal
variable declaration.
– Daniel Pryden
Apr 3 '18 at 13:43
3
3
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
"Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause)." - While true, this not relevant. C and C++ have free / delete operators respectively, and yet the equivalent C / C++ code to your examples would be illegal. C and C++ blocks limit the scope of variables just like in Java. In fact, this is true for most "block structured" languages.
– Stephen C
Nov 1 '17 at 13:41
The better solution for code that assigns a different value on every branch is to use a blank
final
variable declaration.– Daniel Pryden
Apr 3 '18 at 13:43
The better solution for code that assigns a different value on every branch is to use a blank
final
variable declaration.– Daniel Pryden
Apr 3 '18 at 13:43
add a comment |
One way to get this error in Eclipse :
- Define a class
A
insrc/test/java
. - Define another class
B
insrc/main/java
that uses classA
.
Result : Eclipse will compile the code, but maven will give "Cannot find symbol".
Underlying cause : Eclipse is using a combined build path for the main and test trees. Unfortunately, it does not support using different build paths for different parts of an Eclipse project, which is what Maven requires.
Solution :
- Don't define your dependencies that way; i.e. don't make this mistake.
- Regularly build your codebase using Maven so that you pick up this mistake early. One way to do that is to use a CI server.
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
2
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
add a comment |
One way to get this error in Eclipse :
- Define a class
A
insrc/test/java
. - Define another class
B
insrc/main/java
that uses classA
.
Result : Eclipse will compile the code, but maven will give "Cannot find symbol".
Underlying cause : Eclipse is using a combined build path for the main and test trees. Unfortunately, it does not support using different build paths for different parts of an Eclipse project, which is what Maven requires.
Solution :
- Don't define your dependencies that way; i.e. don't make this mistake.
- Regularly build your codebase using Maven so that you pick up this mistake early. One way to do that is to use a CI server.
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
2
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
add a comment |
One way to get this error in Eclipse :
- Define a class
A
insrc/test/java
. - Define another class
B
insrc/main/java
that uses classA
.
Result : Eclipse will compile the code, but maven will give "Cannot find symbol".
Underlying cause : Eclipse is using a combined build path for the main and test trees. Unfortunately, it does not support using different build paths for different parts of an Eclipse project, which is what Maven requires.
Solution :
- Don't define your dependencies that way; i.e. don't make this mistake.
- Regularly build your codebase using Maven so that you pick up this mistake early. One way to do that is to use a CI server.
One way to get this error in Eclipse :
- Define a class
A
insrc/test/java
. - Define another class
B
insrc/main/java
that uses classA
.
Result : Eclipse will compile the code, but maven will give "Cannot find symbol".
Underlying cause : Eclipse is using a combined build path for the main and test trees. Unfortunately, it does not support using different build paths for different parts of an Eclipse project, which is what Maven requires.
Solution :
- Don't define your dependencies that way; i.e. don't make this mistake.
- Regularly build your codebase using Maven so that you pick up this mistake early. One way to do that is to use a CI server.
edited Jul 30 '16 at 1:09
Stephen C
516k70566922
516k70566922
answered May 13 '16 at 10:09
Joel CostigliolaJoel Costigliola
2,1501016
2,1501016
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
2
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
add a comment |
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
2
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
What is the solution to this one?
– user4964330
Jun 15 '16 at 10:27
2
2
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
whatever you use in src/main/java needs to be defined in src/main/java or in any compile/runtime dependencies (not test dependencies).
– Joel Costigliola
Jun 16 '16 at 4:27
add a comment |
"Can not find " means that , compiler who can't find appropriate variable, method ,class etc...if you got that error massage , first of all you want to find code line where get error massage..And then you will able to find which variable , method or class have not define before using it.After confirmation initialize that variable ,method or class can be used for later require...Consider the following example.
I'll create a demo class and print a name...
class demo{
public static void main(String a){
System.out.print(name);
}
}
Now look at the result..
That error says, "variable name can not find"..Defining and initializing value for 'name' variable can be abolished that error..Actually like this,
class demo{
public static void main(String a){
String name="smith";
System.out.print(name);
}
}
Now look at the new output...
Ok Successfully solved that error..At the same time , if you could get "can not find method " or "can not find class" something , At first,define a class or method and after use that..
add a comment |
"Can not find " means that , compiler who can't find appropriate variable, method ,class etc...if you got that error massage , first of all you want to find code line where get error massage..And then you will able to find which variable , method or class have not define before using it.After confirmation initialize that variable ,method or class can be used for later require...Consider the following example.
I'll create a demo class and print a name...
class demo{
public static void main(String a){
System.out.print(name);
}
}
Now look at the result..
That error says, "variable name can not find"..Defining and initializing value for 'name' variable can be abolished that error..Actually like this,
class demo{
public static void main(String a){
String name="smith";
System.out.print(name);
}
}
Now look at the new output...
Ok Successfully solved that error..At the same time , if you could get "can not find method " or "can not find class" something , At first,define a class or method and after use that..
add a comment |
"Can not find " means that , compiler who can't find appropriate variable, method ,class etc...if you got that error massage , first of all you want to find code line where get error massage..And then you will able to find which variable , method or class have not define before using it.After confirmation initialize that variable ,method or class can be used for later require...Consider the following example.
I'll create a demo class and print a name...
class demo{
public static void main(String a){
System.out.print(name);
}
}
Now look at the result..
That error says, "variable name can not find"..Defining and initializing value for 'name' variable can be abolished that error..Actually like this,
class demo{
public static void main(String a){
String name="smith";
System.out.print(name);
}
}
Now look at the new output...
Ok Successfully solved that error..At the same time , if you could get "can not find method " or "can not find class" something , At first,define a class or method and after use that..
"Can not find " means that , compiler who can't find appropriate variable, method ,class etc...if you got that error massage , first of all you want to find code line where get error massage..And then you will able to find which variable , method or class have not define before using it.After confirmation initialize that variable ,method or class can be used for later require...Consider the following example.
I'll create a demo class and print a name...
class demo{
public static void main(String a){
System.out.print(name);
}
}
Now look at the result..
That error says, "variable name can not find"..Defining and initializing value for 'name' variable can be abolished that error..Actually like this,
class demo{
public static void main(String a){
String name="smith";
System.out.print(name);
}
}
Now look at the new output...
Ok Successfully solved that error..At the same time , if you could get "can not find method " or "can not find class" something , At first,define a class or method and after use that..
edited Jul 1 '18 at 3:09
answered Jun 30 '18 at 11:09
GT_hashGT_hash
515
515
add a comment |
add a comment |
If you're getting this error in the build somewhere else, while your IDE says everything is perfectly fine, then check that you are using the same Java versions in both places.
For example, Java 7 and Java 8 have different APIs, so calling a non-existent API in an older Java version would cause this error.
add a comment |
If you're getting this error in the build somewhere else, while your IDE says everything is perfectly fine, then check that you are using the same Java versions in both places.
For example, Java 7 and Java 8 have different APIs, so calling a non-existent API in an older Java version would cause this error.
add a comment |
If you're getting this error in the build somewhere else, while your IDE says everything is perfectly fine, then check that you are using the same Java versions in both places.
For example, Java 7 and Java 8 have different APIs, so calling a non-existent API in an older Java version would cause this error.
If you're getting this error in the build somewhere else, while your IDE says everything is perfectly fine, then check that you are using the same Java versions in both places.
For example, Java 7 and Java 8 have different APIs, so calling a non-existent API in an older Java version would cause this error.
answered Mar 8 '16 at 5:58
Jonathan LinJonathan Lin
11.1k45052
11.1k45052
add a comment |
add a comment |
I too was getting this error. (for which I googled and I was directed to this page)
Problem: I was calling a static method defined in the class of a project A from a class defined in another project B.
I was getting the following error:
error: cannot find symbol
Solution: I resolved this by first building the project where the method is defined then the project where the method was being called from.
add a comment |
I too was getting this error. (for which I googled and I was directed to this page)
Problem: I was calling a static method defined in the class of a project A from a class defined in another project B.
I was getting the following error:
error: cannot find symbol
Solution: I resolved this by first building the project where the method is defined then the project where the method was being called from.
add a comment |
I too was getting this error. (for which I googled and I was directed to this page)
Problem: I was calling a static method defined in the class of a project A from a class defined in another project B.
I was getting the following error:
error: cannot find symbol
Solution: I resolved this by first building the project where the method is defined then the project where the method was being called from.
I too was getting this error. (for which I googled and I was directed to this page)
Problem: I was calling a static method defined in the class of a project A from a class defined in another project B.
I was getting the following error:
error: cannot find symbol
Solution: I resolved this by first building the project where the method is defined then the project where the method was being called from.
edited Oct 7 '16 at 14:09
answered Sep 28 '16 at 14:59
MariaMaria
132213
132213
add a comment |
add a comment |
For hints, look closer at the class name name that throws an error and the line number, example:
Compilation failure
[ERROR] applicationsxxxxx.java:[44,30] error: cannot find symbol
One other cause is unsupported method of for java version say jdk7 vs 8.
Check your %JAVA_HOME%
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
add a comment |
For hints, look closer at the class name name that throws an error and the line number, example:
Compilation failure
[ERROR] applicationsxxxxx.java:[44,30] error: cannot find symbol
One other cause is unsupported method of for java version say jdk7 vs 8.
Check your %JAVA_HOME%
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
add a comment |
For hints, look closer at the class name name that throws an error and the line number, example:
Compilation failure
[ERROR] applicationsxxxxx.java:[44,30] error: cannot find symbol
One other cause is unsupported method of for java version say jdk7 vs 8.
Check your %JAVA_HOME%
For hints, look closer at the class name name that throws an error and the line number, example:
Compilation failure
[ERROR] applicationsxxxxx.java:[44,30] error: cannot find symbol
One other cause is unsupported method of for java version say jdk7 vs 8.
Check your %JAVA_HOME%
answered Sep 6 '17 at 12:47
StrikerStriker
14415
14415
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
add a comment |
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
This just says the same thing that other answers say.
– Stephen C
Jul 6 '18 at 11:03
add a comment |
There can be various scenarios as people have mentioned above. A couple of things which have helped me resolve this.
If you are using IntelliJ
File -> 'Invalidate Caches/Restart'
OR
The class being referenced was in another project and that dependency was not added to the Gradle build file of my project. So I added the dependency using
compile project(':anotherProject')
and it worked. HTH!
add a comment |
There can be various scenarios as people have mentioned above. A couple of things which have helped me resolve this.
If you are using IntelliJ
File -> 'Invalidate Caches/Restart'
OR
The class being referenced was in another project and that dependency was not added to the Gradle build file of my project. So I added the dependency using
compile project(':anotherProject')
and it worked. HTH!
add a comment |
There can be various scenarios as people have mentioned above. A couple of things which have helped me resolve this.
If you are using IntelliJ
File -> 'Invalidate Caches/Restart'
OR
The class being referenced was in another project and that dependency was not added to the Gradle build file of my project. So I added the dependency using
compile project(':anotherProject')
and it worked. HTH!
There can be various scenarios as people have mentioned above. A couple of things which have helped me resolve this.
If you are using IntelliJ
File -> 'Invalidate Caches/Restart'
OR
The class being referenced was in another project and that dependency was not added to the Gradle build file of my project. So I added the dependency using
compile project(':anotherProject')
and it worked. HTH!
edited Aug 18 '18 at 1:50
answered Aug 18 '18 at 1:45
avpavp
630817
630817
add a comment |
add a comment |
If eclipse Java build path is mapped to 7, 8 and in Project pom.xml Maven properties java.version is mentioned higher Java version(9,10,11, etc..,) than 7,8 you need to update in pom.xml file.
In Eclipse if Java is mapped to Java version 11 and in pom.xml it is mapped to Java version 8. Update Eclipse support to Java 11 by go through below steps in eclipse IDE
Help -> Install New Software ->
Paste following link http://download.eclipse.org/eclipse/updates/4.9-P-builds at Work With
or
Add (Popup window will open) ->
Name:
Java 11 support
Location:
http://download.eclipse.org/eclipse/updates/4.9-P-builds
then update Java version in Maven properties of pom.xml file as below
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Finally do right click on project Debug as -> Maven clean, Maven build steps
add a comment |
If eclipse Java build path is mapped to 7, 8 and in Project pom.xml Maven properties java.version is mentioned higher Java version(9,10,11, etc..,) than 7,8 you need to update in pom.xml file.
In Eclipse if Java is mapped to Java version 11 and in pom.xml it is mapped to Java version 8. Update Eclipse support to Java 11 by go through below steps in eclipse IDE
Help -> Install New Software ->
Paste following link http://download.eclipse.org/eclipse/updates/4.9-P-builds at Work With
or
Add (Popup window will open) ->
Name:
Java 11 support
Location:
http://download.eclipse.org/eclipse/updates/4.9-P-builds
then update Java version in Maven properties of pom.xml file as below
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Finally do right click on project Debug as -> Maven clean, Maven build steps
add a comment |
If eclipse Java build path is mapped to 7, 8 and in Project pom.xml Maven properties java.version is mentioned higher Java version(9,10,11, etc..,) than 7,8 you need to update in pom.xml file.
In Eclipse if Java is mapped to Java version 11 and in pom.xml it is mapped to Java version 8. Update Eclipse support to Java 11 by go through below steps in eclipse IDE
Help -> Install New Software ->
Paste following link http://download.eclipse.org/eclipse/updates/4.9-P-builds at Work With
or
Add (Popup window will open) ->
Name:
Java 11 support
Location:
http://download.eclipse.org/eclipse/updates/4.9-P-builds
then update Java version in Maven properties of pom.xml file as below
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Finally do right click on project Debug as -> Maven clean, Maven build steps
If eclipse Java build path is mapped to 7, 8 and in Project pom.xml Maven properties java.version is mentioned higher Java version(9,10,11, etc..,) than 7,8 you need to update in pom.xml file.
In Eclipse if Java is mapped to Java version 11 and in pom.xml it is mapped to Java version 8. Update Eclipse support to Java 11 by go through below steps in eclipse IDE
Help -> Install New Software ->
Paste following link http://download.eclipse.org/eclipse/updates/4.9-P-builds at Work With
or
Add (Popup window will open) ->
Name:
Java 11 support
Location:
http://download.eclipse.org/eclipse/updates/4.9-P-builds
then update Java version in Maven properties of pom.xml file as below
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Finally do right click on project Debug as -> Maven clean, Maven build steps
answered 17 hours ago
pudaykiranpudaykiran
4,80454572
4,80454572
add a comment |
add a comment |
You can also get this error if you're forgot a new as in:
int i = Integer(); ---->> should be "new Integer()"
(This is why I get the error in my case)
Do you mean that declaring a variableBoolean b;
instead ofboolean b;
can cause this error? And to which typestring
are you referring to?
– LuCio
Aug 30 '18 at 14:42
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
This is misleading. Simply usingBoolean
instead ofboolean
orInteger
instead ofint
will not cause this error. You need to do something else as well. BothBoolean
andInteger
are valid type names, and will be understood by the Java compiler.
– Stephen C
Oct 31 '18 at 1:39
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
add a comment |
You can also get this error if you're forgot a new as in:
int i = Integer(); ---->> should be "new Integer()"
(This is why I get the error in my case)
Do you mean that declaring a variableBoolean b;
instead ofboolean b;
can cause this error? And to which typestring
are you referring to?
– LuCio
Aug 30 '18 at 14:42
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
This is misleading. Simply usingBoolean
instead ofboolean
orInteger
instead ofint
will not cause this error. You need to do something else as well. BothBoolean
andInteger
are valid type names, and will be understood by the Java compiler.
– Stephen C
Oct 31 '18 at 1:39
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
add a comment |
You can also get this error if you're forgot a new as in:
int i = Integer(); ---->> should be "new Integer()"
(This is why I get the error in my case)
You can also get this error if you're forgot a new as in:
int i = Integer(); ---->> should be "new Integer()"
(This is why I get the error in my case)
edited Oct 31 '18 at 14:55
answered Aug 29 '18 at 8:22
BlaCk HoLeBlaCk HoLe
575
575
Do you mean that declaring a variableBoolean b;
instead ofboolean b;
can cause this error? And to which typestring
are you referring to?
– LuCio
Aug 30 '18 at 14:42
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
This is misleading. Simply usingBoolean
instead ofboolean
orInteger
instead ofint
will not cause this error. You need to do something else as well. BothBoolean
andInteger
are valid type names, and will be understood by the Java compiler.
– Stephen C
Oct 31 '18 at 1:39
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
add a comment |
Do you mean that declaring a variableBoolean b;
instead ofboolean b;
can cause this error? And to which typestring
are you referring to?
– LuCio
Aug 30 '18 at 14:42
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
This is misleading. Simply usingBoolean
instead ofboolean
orInteger
instead ofint
will not cause this error. You need to do something else as well. BothBoolean
andInteger
are valid type names, and will be understood by the Java compiler.
– Stephen C
Oct 31 '18 at 1:39
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
Do you mean that declaring a variable
Boolean b;
instead of boolean b;
can cause this error? And to which type string
are you referring to?– LuCio
Aug 30 '18 at 14:42
Do you mean that declaring a variable
Boolean b;
instead of boolean b;
can cause this error? And to which type string
are you referring to?– LuCio
Aug 30 '18 at 14:42
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
Yes in my case, I had the same error, and when I changed the variable declaration Boolean b; to boolean b; it works
– BlaCk HoLe
Aug 31 '18 at 8:09
This is misleading. Simply using
Boolean
instead of boolean
or Integer
instead of int
will not cause this error. You need to do something else as well. Both Boolean
and Integer
are valid type names, and will be understood by the Java compiler.– Stephen C
Oct 31 '18 at 1:39
This is misleading. Simply using
Boolean
instead of boolean
or Integer
instead of int
will not cause this error. You need to do something else as well. Both Boolean
and Integer
are valid type names, and will be understood by the Java compiler.– Stephen C
Oct 31 '18 at 1:39
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
You are right @StephenC
– BlaCk HoLe
Oct 31 '18 at 14:57
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
Your example is a dup. Search this Q&A page for "should be 'new String()'" ...
– Stephen C
Oct 31 '18 at 22:21
add a comment |