When I use javac to complile several .java files, I got some "duplicate class" errors, but I can't find errors in my code.
There are four .java files, all these files are in the same folder in Windows.
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();
}
}
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);
}
}
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);
}
}
package dx;
public class Worker {
public void Speak(){
System.out.println("I'm a worker.");
}
}
In Windows command line, I use javac to compile these source codes:
javac MyApp.java Rectangle.java Circle.java Worker.java
But the only thing I got is a list of errors:
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
I don't know what is wrong. Why?
Please refer to the documentation of compiler: Arrangement of Source Code
Mainly:
When classes and interfaces are organized into a package, the package is represented as a directory, and any subpackages are represented as subdirectories.
Assuming your root source directory is src
, the files should be arranged as
src/
|
+ - MyApp.java
|
+ = dx/
|
+ - Worker.java
|
+ = shapes/
|
+ - Circle.java
+ - Rectangle.java
To compile change to the src
directory and use:
/src> javac *.java dx/*.java dx/shapes/*java
or, for Windows:
C:\src>javac *.java dx\*.java dx\shapes\*java
Since all classes are being referenced in MyApp
, you can just compile that file and the compiler will find and compile the other classes:
src> javac MyApp.java
Better compile all files at once since (same documentation):
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.