繁体   English   中英

Java无法在同一包中找到类,也无法导入其他包

[英]Java cannot find class in the same package neither import other packages

我们的教授希望我们使用命令提示符而不是像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

在此先感谢大家

首先,如果您在使用Javac时没有构建工具的帮助,则需要按目录编译Java文件目录

javac src / poo / util / *。java -d bin /

请注意,使用-d时足以提供根输出文件夹,从以上命令生成的Mat.class文件将在bin / poo / util中找到

在其他类中导入Mat时,需要在编译它们时确保它在您的类路径中。

javac src / poo / math / *。java -d bin / -cp bin /

这样可以很好地编译,并将Point和Polygon类放入bin / poo / math

暂无
暂无

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

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