简体   繁体   English

javac类路径找不到符号?

[英]javac classpath cannot find symbol?

i have two classes at the same folder:我在同一个文件夹中有两个类:

  • First.java一、java
  • Second.java二.java

First class implementation:第一个 class 实现:

package mypackage;

public class First {
    public static void main(String[] args) {
        Second.myMethod();
    }

}

Second class implementation:第二个 class 实现:

public class Second {
    public static void myMethod() {
        System.out.println("this is the second class");
    }
}

the Second has been compiled, so it becomes:第二个已经编译,所以它变成:

  • First.java一、java
  • Second.java二.java
  • Second.class二.class

when i try to compile First.java: javac -cp.;.. -d.当我尝试编译 First.java: javac -cp.;.. -d. First.java一、java

i got an error:我有一个错误:

cannot find symbol
                 Second.myMethod();
                        ^
  symbol:   variable Second
  location: class First
1 error

i have tried: "javac -d. *.java" and i got the same result我试过:“javac -d.*.java”,我得到了同样的结果

Starting point初始点

You showed two class definitions..您展示了两个 class 定义..

  • Here is First, which belongs to package "mypackage":这里是First,属于package“mypackage”:
     package mypackage; public class First { public static void main(String[] args) { Second.myMethod(); } }
  • Here is Second, which has no package statement:这是第二个,它没有 package 语句:
     public class Second { public static void myMethod() { System.out.println("this is the second class"); } }

If you attempt to compile those together, you'll get a compilation error like below (OpenJDK 18.0.1.1):如果您尝试将它们编译在一起,您将收到如下所示的编译错误(OpenJDK 18.0.1.1):

% javac *.java
First.java:5: error: cannot find symbol
        Second.myMethod();
        ^
  symbol:   variable Second
  location: class First
1 error

Why it doesn't work为什么它不起作用

From 7.4.2.7.4.2 开始。 Unnamed Packages : 未命名的包

A compilation unit that has no package declaration is part of an unnamed package.没有 package 声明的编译单元是未命名的 package 的一部分。

Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development.未命名的软件包由 Java SE 平台提供,主要是为了方便开发小型或临时应用程序或刚开始开发时。

This describes "Second" – a compilation unit which has no package declaration.这描述了“第二”——一个没有 package 声明的编译单元。 JLS 7.4.2 tells us that this class would belong to an unnamed package. JLS 7.4.2 告诉我们这个 class 将属于一个未命名的 package。 Further, we can see that unnamed packages are not intended to be used for much of anything.此外,我们可以看到未命名的包并不打算用于任何事情。

From 7.5.7.5 开始。 Import Declarations : 进口申报

Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name ( §6.7 ).在不使用适当的导入声明的情况下,引用在另一个 package 或另一个类型的 static 成员中声明的类型的唯一方法是使用完全限定名称( 第 6.7 节)。

This tells us that in First.java, if we wish to refer to a type defined in another package, we would need to use the fully qualified name of the type we wish to reference.这告诉我们,在 First.java 中,如果我们希望引用另一个 package 中定义的类型,我们需要使用我们希望引用的类型的完全限定名称。

This is what you tried to do – First is in named package "mypackage", and Second is in an unnamed package.这就是你试图做的——首先是在名为 package “mypackage”中,其次是在未命名的 package 中。 For First to see Second, First would need to use a fully-qualified name for Second.要让 First 看到 Second,First 需要为 Second 使用完全限定的名称。 But there is no fully-qualified name for Second – it belongs to an unnamed package;但是 Second 没有完全限定的名称——它属于一个未命名的 package; it doesn't have a name, so it isn't possible to reference it.它没有名称,因此无法引用它。


Solution 1解决方案 1

Edit Second – move it from the unnamed package to the named package "mypackage" by putting this at the top: package mypackage;编辑第二 - 将其从未命名的 package 移动到命名的 package “mypackage”,方法是将其放在顶部: package mypackage; . . By doing this, both First and Second would belong to the same named package, and compilation will succeed.通过这样做,First 和 Second 将属于同一个名称的 package,编译将成功。


Solution 2解决方案 2

Edit First – move it from the named package "mypackage" to the unnamed package by commenting out or deleting this line: package mypackage;首先编辑 - 通过注释掉或删除此行,将其从命名的 package “mypackage”移动到未命名的 package: package mypackage; . . By doing this, you are removing the named package altogether, leaving First and Second both in the unnamed package, and compilation will succeed.通过这样做,您将完全删除命名的 package,将 First 和 Second 都留在未命名的 package 中,编译将成功。

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

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