我初学java,学到关于包的内容遇到了问题以下分别是三个.java文件//第一个文件Point.java
package graphics.twoD;public class Point{ public int x=0;
public int y=0;
public Point(int x, int y){ this.x=x;
this.y=y;
}
}//第二个文件Rectangle.java
package graphics.twoD;public class Rectangle{ public int width=0;
public int height=0;
public Point origin;
public Rectangle(Point p, int w, int h){ origin=p;
width=w;
height=h; }
public void move(int x, int y){ origin.x=x;
origin.y=y;
} public int area(){ return width*height;
}
}//第三个文件TestPackage.java
import graphics.twoD.*;
public class TestPackage{ public static void main(String args[]){ Point p=new Point(2,3);
Rectangle r=new Rectangle(p,10,10);
System.out.println("The area of the rectangle is"+r.area());
}
}我将环境变量classpath路径设为: f:\java\class以上三个文件均位于f:\java\source目录下第一步:f:\java\source目录下输入javac -d f:\java\class Point.java Rectangle.java 回车后,
在f:\java\class\graphics\twoD下生成类文件Point和Rectangle第二步:f:\java\source目录下输入javac -d f:\java\class\sample TestPackage.java 回车后,
在f:\java\class\sample下生成类TestPackage第三步:输入java -classpath f:\java\class\sample TestPackage 回车后却提示错误:Exception in thread "main" java.lang.NoClassDefFoundError: graphics/twoD/Point
at TestPackage.main(testpackage.java:8)
不知何故?
怎样改正?
package graphics.twoD;public class Point{ public int x=0;
public int y=0;
public Point(int x, int y){ this.x=x;
this.y=y;
}
}//第二个文件Rectangle.java
package graphics.twoD;public class Rectangle{ public int width=0;
public int height=0;
public Point origin;
public Rectangle(Point p, int w, int h){ origin=p;
width=w;
height=h; }
public void move(int x, int y){ origin.x=x;
origin.y=y;
} public int area(){ return width*height;
}
}//第三个文件TestPackage.java
import graphics.twoD.*;
public class TestPackage{ public static void main(String args[]){ Point p=new Point(2,3);
Rectangle r=new Rectangle(p,10,10);
System.out.println("The area of the rectangle is"+r.area());
}
}我将环境变量classpath路径设为: f:\java\class以上三个文件均位于f:\java\source目录下第一步:f:\java\source目录下输入javac -d f:\java\class Point.java Rectangle.java 回车后,
在f:\java\class\graphics\twoD下生成类文件Point和Rectangle第二步:f:\java\source目录下输入javac -d f:\java\class\sample TestPackage.java 回车后,
在f:\java\class\sample下生成类TestPackage第三步:输入java -classpath f:\java\class\sample TestPackage 回车后却提示错误:Exception in thread "main" java.lang.NoClassDefFoundError: graphics/twoD/Point
at TestPackage.main(testpackage.java:8)
不知何故?
怎样改正?
java -classpath "f:\java\class\sample;f:\java\class\graphics\twoD" TestPackage