gpt4 book ai didi

java - 当我使用 javac 编译多个 .java 文件时,出现一些 "duplicate class"错误,但我在代码中找不到错误

转载 作者:行者123 更新时间:2023-11-30 01:44:28 25 4
gpt4 key购买 nike

当我使用 javac 编译多个 .java 文件时,出现一些“重复类” 错误,但我在代码中找不到错误。

有四个.java文件,所有这些文件都位于Windows的同一个文件夹中。

  1. MyApp.java 文件中的代码:
import dx.*;
import dx.shapes.*;

class MyApp {
public static void main(String[] args) {
System.out.println("This is a test application.");

Rectangle rect = new Rectangle(10, 20);
rect.Speak();

Circle circle = new Circle(15);
circle.Speak();

Worker worker = new Worker();
worker.Speak();
}
}
  • Rectangle.java 文件中的代码:
  •     package dx.shapes;

    public class Rectangle {
    private int x, y;
    private int width, height;

    public Rectangle() {
    this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
    this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    }
    public void Speak(){
    System.out.println("I'm a rectangle, width:" + this.width + ", height:" + this.height);
    }
    }
  • Circle.java 文件中的代码:
  • package dx.shapes;

    public class Circle {
    private int x, y;
    private int radius;

    public Circle() {
    this(0, 0, 10);
    }
    public Circle(int radius) {
    this(0, 0, radius);
    }
    public Circle(int x, int y, int radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    }
    public void Speak(){
    System.out.println("I'm a circle, radius:" + this.radius);
    }
    }
  • Worker.java 文件中的代码:
  • package dx;

    public class Worker {
    public void Speak(){
    System.out.println("I'm a worker.");
    }
    }

    在Windows命令行中,我使用javac来编译这些源代码:

    javac MyApp.java Rectangle.java Circle.java Worker.java

    但我得到的唯一的东西是错误列表:

    Rectangle.java:3: error: duplicate class: dx.shapes.Rectangle
    public class Rectangle {
    ^
    MyApp.java:8: error: cannot access Rectangle
    Rectangle rect = new Rectangle(10, 20);
    ^
    bad source file: .\Rectangle.java
    file does not contain class Rectangle
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
    Circle.java:3: error: duplicate class: dx.shapes.Circle
    public class Circle {
    ^
    MyApp.java:11: error: cannot access Circle
    Circle circle = new Circle(15);
    ^
    bad source file: .\Circle.java
    file does not contain class Circle
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
    Worker.java:3: error: duplicate class: dx.Worker
    public class Worker {
    ^
    MyApp.java:14: error: cannot access Worker
    Worker worker = new Worker();
    ^
    bad source file: .\Worker.java
    file does not contain class Worker
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
    6 errors

    我不知道出了什么问题。为什么?

    最佳答案

    请引用编译器的文档:Arrangement of Source Code

    主要:

    When classes and interfaces are organized into a package, the package is represented as a directory, and any subpackages are represented as subdirectories.

    假设您的根源目录是src,文件应排列为

    src/
    |
    + - MyApp.java
    |
    + = dx/
    |
    + - Worker.java
    |
    + = shapes/
    |
    + - Circle.java
    + - Rectangle.java

    要将更改编译到 src 目录并使用:

    /src> javac *.java dx/*.java dx/shapes/*java

    或者,对于 Windows:

    C:\src>javac *.java dx\*.java dx\shapes\*java

    由于 MyApp 中引用了所有类,因此您只需编译该文件,编译器就会找到并编译其他类:

    src> javac MyApp.java

    更好地立即编译所有文件,因为(相同的文档):

    The order of source files specified on the command line or in an argument file is not important. javac will compile the files together, as a group, and will automatically resolve any dependencies between the declarations in the various source files.

    关于java - 当我使用 javac 编译多个 .java 文件时,出现一些 "duplicate class"错误,但我在代码中找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58643709/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com