找不到同一包中的文件

FIle in the same Package is not Found

我正在使用 spring 框架。 我有一个接口和一个 class 在同一个包中。
我的界面是

package soundsystem;

public interface CompactDisc{
    void play();
}

我的class是

package soundsystem;
import org.springframework.stereotype.*;

@Component
public class Sgtpeppers implements CompactDisc{
    private String title = "A Movie";
    private String artist = "The Movie is Being Played";

    public void play(){
        System.out.println("The CD is Played \n"+title+"\n"+artist);
    }
}

编译时出现这个错误

Sgtpeppers.java:5: error: cannot find symbol
public class Sgtpeppers implements CompactDisc{
                                   ^
symbol: class CompactDisc
1 error

首先编译接口,.class文件也存储了声音系统包。

我认为 javac 命令有问题。
我使用的命令是

javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java 

这是因为我更改了 class路径吗?

1) 作为一般规则,要编译 classes,不要从特定包的目录执行任务。
相反,使用应用程序源代码的根作为 运行 javac/java 命令的基础。

遵循这个简单的规则:

2)要编译位于同一包中的所有 classes,只需将包指定为 "source files" :

javac soundsystem/*.java

3)要根据另一个已编译的 class 源代码(是否在同一包中)编译特定的 class,您不需要指定"."(表示当前目录)在 class 路径中,因为它是默认值 .

Java documentation 确实指出:

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, then you must include a dot (.) in the new settings.

但是如果你显式地给class路径设置了另一个值,默认值就不再被使用了。
你做到了:

javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java

因此您应该明确添加“.”在 class 路径中。

从源代码根目录,它会给出 Windows :

javac -d . -cp ".;spring-framework-5.0.1.RELEASE/libs/*" soundsystem/Sgtpeppers.java

对于 Unix,分隔符字符是 :,因此它会给出:

javac -d . -cp ".:spring-framework-5.0.1.RELEASE/libs/*" soundsystem/Sgtpeppers.java