什么是“找不到符号”编译错误是什么意思? [英] What does a "Cannot find symbol" compilation error mean?

查看:1447
本文介绍了什么是“找不到符号”编译错误是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释下列有关找不到符号的错误:




  • 此错误意味着什么?
  • $ b



  • b $ b

    这个问题是一个关于无法找到符号编译错误的综合问题。

    解决方案

    1。 无法找到符号错误是什么意思?



    首先,它是一个编译错误 1 。这意味着 在您的Java源代码中存在问题,在编译它时出现问题。



    您的Java源代码包含以下内容:




    • 关键字:like true false class while ,等等。

    • 字面:如 42 'X'

    • 操作符和其他非字母数字标记:例如 code>, = {等。

    • 标识符: Reader i toString code> processEquibalancedElephants ,等等。

    • 评论和空白。


    b $ b

    无法找到符号错误是关于标识符的。当你的代码被编译时,编译器需要计算代码中的每个标识符。



    无法找到符号错误表示编译器无法执行此操作。您的代码似乎指的是编译器不能理解的内容。



    2。什么原因可能导致找不到符号错误?



    作为第一个订单,只有一个原因。编译器在所有要定义标识符的地方查找,并且找不到定义。这可能是由一些事情造成的。常见的如下:




    • 对于一般的标识符:


      • 也许你拼写错了名字;即 StringBiulder 而不是 StringBuilder 。 Java不能也不会尝试补偿拼写错误或输入错误。

      • 也许你错了,即 stringBuilder 而不是 StringBuilder 。所有Java标识符都区分大小写。

      • 也许你不适当地使用下划线;即 mystring my_string 不同。 (如果你坚持使用Java风格的规则,你将在很大程度上免受这个错误...)


    • 到变量:


      • 也许你忘了声明变量。

      • 也许变量声明超出范围点你试图使用它。 (见下面的示例)


    • 对于应该是方法名的标识符:

        $ b $ >

        对于应该是类名的标识符:




        • 可能您忘了导入类。

        • 也许您使用了「星号」汇入,但未在您汇入的任何套件中定义类别。

        • 也许您忘记了 new 如:

            String s = String //应为'new String()'





          问题通常是上述的组合。例如,也许你导入 java.io。* ,然后尝试使用 Files 。它在 java.nio 而不是 java.io 。或者你可能想写 File ...其中中的类 >






          下面是一个例子,说明不正确的变量范围导致Can not find symbol错误: p>

            for(int i = 0; i< strings.size(); i ++){
          if(strings.get (i).equalsIgnoreCase(fnoord)){
          break;
          }
          }
          if(i< strings.size()){
          ...
          }

          这将给中的 i if 语句。虽然我们先前声明了 i ,但是声明仅在 for 语句的范围其身体。在 if 语句 i 的引用看不到 i 。这是超出范围



          (这里适当的修正可能是移动 if 语句,或者在循环开始之前声明 i 。)






          如果从命令行编译,编译器可能找不到符号的另一个原因。你可能只是忘了编译或重新编译一些其他类。例如,如果您有 Foo Bar Foo 使用 Bar 。如果你从来没有编译过 Bar ,并运行 javac Foo.java ,你会发现编译器可以'找到符号 Bar 。简单的答案是 Foo Bar 在一起;例如 javac Foo.java Bar.java javac * .java 。或者更好的使用Java构建工具;例如Ant,Maven,Gradle等。



          还有一些其他更加模糊的原因...我会在下面处理。



          3。如何解决这些错误?



          一般来说,你首先要弄明白这个问题是什么造成的。然后您可以思考您的代码应该说什么。然后最后,你需要对你的源代码做出什么校正来做你想要的。



          请注意,并非每个校正都是正确的。考虑这个:

            for(int i = 1; i <10; i ++){
          for(j = 1; j <10; j ++){
          ...
          }
          }


          b $ b

          假设编译器对 j 说无法找到符号。有很多方法可以修复:




          • 我可以改变内部 for for(int j = 1; j< 10; j ++) - 可能正确。

          • 对于 j 之前内部循环,或外部 c / code> c>

          • > b


          重要的是,您需要才能理解您的代码为了找到正确的解决方案而尝试做什么。



          4。模糊的原因



          这里有几个例子,找不到符号似乎莫名其妙...直到你看起来更近。


          1. 您正在查看错误的源代码:经常发生的情况是,一个新的Java程序员不了解Java工具链的工作原理,或尚未实施可重复的构建过程;例如使用IDE,Ant,Maven,Gradle等。在这种情况下,程序员可以最终追踪他的尾巴,寻找由于没有正确地重新编译代码而导致的实际的错误。


          2. IDE问题:人们报告了他们的IDE被混淆的情况,IDE中的编译器无法找到存在的类...或相反的情况。




            • 如果IDE的缓存与文件系统不同步,就会发生这种情况。有特定的IDE解决方法。


            • 这可能是一个IDE错误。例如@Joel Costigliola描述了一个场景,其中Eclipse不能正确处理Maven的测试树;请参阅 http://stackoverflow.com/a/372​​07223/139985



          3. 重新定义系统类:我看到编译器提示 substring 是如下所示的未知符号

              String s = ... 
            String s1 = sstrstring(1);



            原来,程序员已经创建了自己的版本 String ,并且他的类的版本没有定义 substring 方法。




          4. Homoglyphs:如果您使用UTF-8编码对于您的源文件,可能具有相同的标识符,但实际上是不同的,因为它们包含同形文本。有关详细信息,请参见此页



            您可以通过限制自己使用ASCII或Latin-1作为源文件编码,并使用Java \uxxxx 转义其他字符。







          1 - 如果您在运行时异常或错误消息中看到此错误消息,那么您已将IDE配置为运行带有编译错误的代码,或者您的应用程序正在运行时生成和编译代码..


          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.

          解决方案

          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 of StringBuilder. Java cannot and will not attempt to compensate for bad spelling or typing errors.
            • Perhaps you got the case wrong; i.e. stringBuilder instead of StringBuilder. All Java identifiers are case sensitive.
            • Perhaps you used underscores inappropriately; i.e. mystring and my_string are different. (If you stick to the Java style rules, you will be largely protected from this mistake ...)
          • 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 names:
            • Perhaps you are trying to refer to an inherited method that wasn't declared in the parent / ancestor classes or interfaces.
          • 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()'
              

          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.)


          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 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 error ?

          Generally speaking, you start out by figuring out what caused the problem. 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 to for (int j = 1; j < 10; j++) - probably correct.
          • I could add a declaration for j before the inner for loop, or the outer for loop - possibly correct.
          • I could change j to i in the inner for 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.

          1. You are looking at the wrong source code: It often happens that a 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 ...

          2. 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 http://stackoverflow.com/a/37207223/139985.

          3. 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 a substring methods.

            Lesson: Don't define your own classes with the same names as common library classes!

          4. 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.

          这篇关于什么是“找不到符号”编译错误是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆