简体   繁体   English

编译 java 时找不到符号

[英]cannot find symbol when compiling java

I have few classes in the same package demo, but the most important are Server.java and MyServerImpl.java.我在同一个 package 演示中的课程很少,但最重要的是 Server.java 和 MyServerImpl.java。 In Server.java I have this code在 Server.java 我有这个代码

package demo;

public class Server
{public static void main( String[] args ) throws Exception
        {
MyServerImpl s = new MyServerImpl("blablabla");
...
}

And the class MyServerImpl和 class MyServerImpl

package demo;

public class MyServerImpl extends MyServerPOA {

        private String location;

        public MyServerImpl( String location )
        {
            this.location = location;
        }

        public void add ( String value ){
            System.out.println("name " + value + location );

        }
            }

When I try to compile with javac I have this error当我尝试使用 javac 编译时出现此错误

cannot find symbol
symbol  : class MyServerImpl
location: class demo.Server
            MyServerImpl s = new MyServerImpl("blablabla");

I try to compile with this command javac project/demo/Server.java and when I am in the demo folder javac Server.java but still get the same error我尝试使用此命令javac project/demo/Server.java并且当我在演示文件夹javac Server.java中时,但仍然得到相同的错误

To expand on my comment: 扩展我的评论:

When javac looks for classes, the folders it looks in are based off of the fully qualified name for the class. javac查找类时,它查找的文件夹基于该类的完全限定名称。

So in your case, the fully qualified name for MyServerImpl is demo.MyServerImpl . 因此,在您的情况下, MyServerImpl的完全限定名称为demo.MyServerImpl Thus, when the compiler encounters MyServerImpl in Server.java , it looks for a folder called demo , and if it finds that folder, looks for MyServerImpl.java . 因此,当编译器在Server.java遇到MyServerImpl时,它将查找一个名为demo的文件夹,如果找到该文件夹​​,则将查找MyServerImpl.java

If you cd 'd into demo, this will cause compilation to fail, because demo obviously doesn't contain itself (otherwise you might have some broken OSs). 如果将cd 装入演示,这将导致编译失败,因为demo显然不包含自身(否则您可能会遇到一些故障的OS)。

So what you need to do is cd into the folder above demo . 因此,您需要做的工作就是将cd放入demo 上面的文件夹中。 So for example, if your file hierarchy is src as root, with the demo package inside, with Server.java and MyServerImpl.java inside: 因此,例如,如果您的文件层次结构是src作为根,则在其中包含demo包,在其中包含Server.javaMyServerImpl.java

  1. cd into src , so that if you ls / dir , demo is (one of) the folder(s) that shows up cd进入src ,因此,如果您使用ls / dir ,则demo是显示的文件夹之一。
  2. javac demo/Server.java
  3. Done (hopefully) 完成(希望如此)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM