简体   繁体   中英

Java execution not working with -classpath

Test.java

package test;

 import shape.twod.*;
 import shape.threed.*;
 import shape.*;

public class Test {

    /**
     * Creates a new instance of <code>Test</code>.
     */
     int o;
    public Test() {

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ObjActions obj[] = new ObjActions[4];
        obj[0] = new Line(1,2,3,4);
        obj[1] = new Circle(1,2,3);
        obj[2] = new Line3D(1,2,3,4,5,6);
        obj[3] = new Sphere(1,2,3,4);
        for(ObjActions x: obj)
            x.draw();
        ObjActions.Actions2D o =(Circle)obj[1];
        //Actions2D o =(Circle)obj[1];
        System.out.println("Area of circle "+o.area());
        ObjActions.Actions3D op = (Sphere)obj[3];
        System.out.println("Volume of sphere "+op.volume());
    }
}

Its location is D:\\Program\\Javalearningprograms and the location of packages used is D:\\Program\\Javalearningprograms\\PackageCheck

First I compiled it with

javac -classpath .\\PackageCheck -d .\\ Test.java

It compiled successfully and created Test.class in .\\test , then I used this

java -classpath .\\PackageCheck test.Test

and got Error: Could not find or load main class test.Test

so I tried with the full path:

java -classpath D:\\Program\\Javalearningprograms\\PackageCheck\\ test.Test

and

java -classpath D:\\Program\\Javalearningprograms\\PackageCheck test.Test

still got Error: Could not find or load main class test.Test

So then to check weather the .class file has any errors I moved folder .\\test to D:\\Program\\Javalearningprograms\\PackageCheck and tried

java test.Test from D:\\Program\\Javalearningprograms\\PackageCheck and the program ran successfully

then I set CLASSPATH environment variable to D:\\Program\\Javalearningprograms\\PackageCheck and cleaned the .class files and then tried

javac -classpath .\\PackageCheck -d .\\ Test.java it created Test.class in .\\test folder and the I used

java test.Test and the program ran successfully, I tried

java -classpath .\\PackageCheck test.Test

java -classpath .\\PackageCheck\\ test.Test

java -classpath D:\\Program\\Javalearningprograms\\PackageCheck test.Test

java -classpath D:\\Program\\Javalearningprograms\\PackageCheck\\ test.Test

and got Error: Could not find or load main class test.Test

I dont know why -classpath is not working with java command, what am I doing wrongly?

    Directory: D:\Program\Javalearningprograms\PackageCheck


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        09-Dec-15   1:35 PM                baseobj
d-----        09-Dec-15   1:35 PM                shape
-a----        09-Dec-15   1:33 PM            559 Circle.java
-a----        09-Dec-15   1:33 PM            566 Line.java
-a----        09-Dec-15   1:33 PM            627 Line3D.java
-a----        09-Dec-15   1:32 PM            384 ObjActions.java
-a----        08-Dec-15   9:58 PM            340 Point.java
-a----        08-Dec-15   9:58 PM            302 Point3D.java
-a----        08-Dec-15  10:05 PM            343 PointTest.java
-a----        09-Dec-15   1:33 PM            547 Sphere.java

Because the Test.class is not in a directory of your classpath.

javac -classpath .\PackageCheck -d .\ Test.java

Creates the file test\\Test.class .

But with your defined classpath -classpath .\\PackageCheck it cannot be found in directory test\\ .

Change your command to

java -classpath .\PackageCheck;. test.Test

This will find classes below PackageCheck\\ and the current directory.

edit Based on the amended question, it seems PackageCheck\\ contains only source files. So all generated class files will be stored below the current directory as

shape\twod\*.class
shape\threed\*.class
shape\*.class
test\*.class

Either you run your code with java -cp . test.Test java -cp . test.Test or store the compiled classes into a dedicated directory (must be created before) javac -cp .\\PackageCheck -d bin Test.java . Then you can run your code with java -cp bin test.Test . (Keep in mind to remove already created class files to be sure they are not taken from a wrong directory.)

See http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html :

The default class path is the current directory. [...] Using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "."

That should explain what you observed - by default, your class is at test/Test.class but as soon as you add the -classpath option you need to include the current directory, like

java -classpath .\PackageCheck;. test.Test

My moving the test/Test.class file below the PackageCheck directory, java was again able to find it even without the current directory being part of the classpath. Remember that the -classpath option defines the root paths where to look for the fully qualified java classes - with the detail that the current directory is not automatically included anymore once you use -classpath .

Try the following

javac -d PackageCheck Test.java

and after compiling

java -cp PackageCheck test.Test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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