2

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.

wzj7531
  • 37
  • 7
  • Why you use `/` in javac and the previous `cd` command and backshlash for all other? – Jens Jan 16 '19 at 08:34
  • Because I am already in D:\MyTest and -cp means specify user class path accoring java document.Please check the document referenced above,you will get the point. – wzj7531 Jan 16 '19 at 08:45
  • 2
    Does it help if you surround it with quotes (`-cp ".;D:/PackageTest/classes"`)? – Thilo Jan 16 '19 at 08:54
  • Try `java -cp ".;D:/PackageTest/classes" TestCircle`. I.e., forward slashes and within quotes – Robert Kock Jan 16 '19 at 08:55
  • Possible duplicate of [Including all the jars in a directory within the Java classpath](https://stackoverflow.com/questions/219585/including-all-the-jars-in-a-directory-within-the-java-classpath) – Janez Kuhar Jan 16 '19 at 09:04
  • Yes surrounding it with quotes works! – wzj7531 Jan 16 '19 at 09:36

1 Answers1

0

It seems to me it's an error not from java but your command shell. Try putting quotes around the value of the -cp argument. Like so:

>java -cp ".;D:\PackageTest\classes" TestCircle

It will instruct the shell to not try to interpret it as a command but just pass it to the java program as is.

Erik
  • 74
  • 6