I am studying the compilation of Java classes with package names according to a document: http://www.ntu.edu.sg/home/ehchua/programming/java/j9c_packageclasspath.html. Based on the content of this article, I created the following directory structure:
+MyTest
-TestCircle.clsss
-TestCircle.java
+PackageTest
+classes
+com
+yyy
-Circle.class
+src
+com
+yyy
-Circle.java
In the above directory structure, "+" represents the folder and "-" represents the java file.The contents in path "\PackageTest\classes" directory are automatically generated when compiling Circle.java files using the javac command. Circle.java uses the package name "com. yyy",here is the Circle code:
package com.yyy;
public class Circle{
private double radius;
public Circle(double radius){this.radius = radius;}
public double getRadius(){return radius;}
public void setRadius(double radius){this.radius = radius;}
public String toString(){return "Circle[radius=" + radius + "]";}
}
And TestCircle code:
import com.yyy.Circle;
public class TestCircle{
public static void main(String[] args){
Circle c1 = new Circle(1.23);
System.out.println(c1);
}
}
I have successfully compiled Circle.java using the following steps:
>cd PackageTest\src\com\yyy
>javac -d classes src\com\yyy\Circle.java
After compilation, the directory structure corresponding to the package name is automatically generated, which is located in "PackageTest\classes" folder. Then I successfully compiled TestCircle.java using the following steps:
>cd MyTest
>javac -cp D:\PackageTest\classes TestCircle.java
But when I executed with the following command, the error occurred:
>java -cp .;D:\PackageTest\classes TestCircle
"The item 'D:\PackageTest\classes' cannot be recognized as the name of cmdlet, function, script file or runnable program".It seems java recognize classpath as some commands.
Any advice will be appreciate!
PS: All the steps above is on my windows 10 Notebook computer.And I don't set CLASSPATH environment variable.