我们的教授希望我们使用命令提示符而不是像Eclipse这样的编辑器进行编译。我遇到很多问题,但我不明白为什么。我有以下三个简单的类:
package poo.math;
public class Point {
private double x, y;
private static int count = 0;
public Point () {
this(0, 0);
count ++;
}
public Point (double x, double y) {
this.x = x;
this.y = y;
count ++;
}
public Point (Point p) {
this.x = p.x;
this.y = p.y;
count ++;
}
protected void finalize () {
count --;
}
public static int pointsCreated () {
return count;
}
public double getX () {
return x;
}
public double getY () {
return y;
}
public void move (double x, double y) {
this.x = x;
this.y = y;
}
public double distance (Point p) {
return Math.sqrt((p.x - this.x)*(p.x -this.x) + (p.y - this.y)*(p.y - this.y));
}
public static void main (String ... args) {
Point p0 = new Point ();
System.out.println(p0);
}
}
package poo.math;
public class Polygon {
private Point [] vertices;
private static int count = 0;
public Polygon (Point ... p) {
vertices = p;
count ++;
}
public Polygon ( Polygon p) {
this.vertices = p.getVertices();
count ++;
}
public Point [] getVertices () {
Point [] res = new Point [this.vertices.length];
for (int i = 0; i < vertices.length; i++) {
res[i] = new Point(vertices[i]);
}
return res;
}
protected void finalize () {
count --;
}
public static int createdPolygons () {
return count;
}
public double perimeter () {
double perimeter = 0;
for (int i = 0; i < vertices.length; i++) {
perimeter += vertices[i].distance(vertices[(i+1) % vertices.length]);
}
return perimeter;
}
public double [] sides () {
double [] sides = new double [vertices.length];
for (int i = 0; i < vertices.length; i++) {
sides[i] = vertices[i].distance(vertices[(i+1) % vertices.length]);
}
return sides;
}
private double triangleArea (Point p0, Point p1, Point p2) {
double a = p0.distance(p1);
double b = p1.distance(p2);
double c = p2.distance(p0);
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
public double area () {
// set p0 at index 0
double a = 0;
for (int i = 1; i < vertices.length - 1; i++) {
a += triangleArea(vertices[0], vertices[i], vertices[i+1]);
}
return a;
}
}
这两个在同一个软件包中,数学。在同一包中,我不需要将Point导入到Polygon中,但这会引发错误:找不到符号。 另外,第三类在另一个包装中
package poo.util;
public final class Mat{
private Mat(){}
public static int mcm( int n, int m ){
if( n<=0 || m<=0 ) throw new IllegalArgumentException();
return (n*m)/mcdAlgorithm(n,m);
}//mcm
public static int mcd( int n, int m ){
if( n<=0 || m<=0 ) throw new IllegalArgumentException();
return mcdAlgorithm(n,m);
}//mcd
private static int mcdAlgorithm( int n, int m ){
if( m==0 ) return n;
return mcdAlgorithm(m,n % m);
}
public static int max (int ... v) {
int m = v[0];
for (int i = 0; i < v.length; i++) if (v[i] > m) m = v[i];
return m;
}
public static void main (String [] args) {
int mcd = mcd(5, 7);
System.out.println(mcd);
}
}//Mat
如果我尝试将Mat类导入其他两个类之一,则会收到错误消息:包poo.util不存在。 这是路径:
Documents
-Java
--src
---poo
----math
-----Point.java
-----Polygon.java
----util
-----Mat.java
--bin
---poo
----math
-----Point.class
-----Polygon.class
----util
-----Mat.class
这些是编译和执行的命令: src> javac -d .. \ bin poo \ math \ Polygon.java bin> java poo.math.Polyhon
我认为这是环境变量或类路径的问题,但我不知道该怎么解决。有人可以帮我吗?
在环境变量中,我只有一个JAVA_HOME变量,其中包含我放入系统路径中的jdk路径,如下所示: %JAVA_HOME%\ bin
先谢谢大家
答案 0 :(得分:0)
首先,如果您在使用Javac时没有构建工具的帮助,则需要按目录编译Java文件目录
javac src / poo / util / *。java -d bin /
请注意,在使用-d时足以提供根输出文件夹,可以在bin / poo / util
中找到根据上述命令生成的Mat.class文件。在其他类中导入Mat时,需要在编译它们时确保它在您的类路径中。
javac src / poo / math / *。java -d bin / -cp bin /
这样可以很好地编译,并将Point和Polygon类放入bin / poo / math