2

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
1
  • Can we see what you have in "D:\Program\Javalearningprograms\PackageCheck\" (a dir /s) Commented Dec 9, 2015 at 12:20

3 Answers 3

2

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 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.)

Sign up to request clarification or add additional context in comments.

3 Comments

@Sab See my upated answer.
I have done javac -d .\ *.java from PackageCheck
I was using Powershell and then I tried java -classpath .\PackageCheck;. test.Test in cmd and it worked
1

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.

5 Comments

What JVM/JDK (vendor and version) on which Operating system are you using? From the paths you showed, I was assuming Windows ... If unsure about java, use java -version
java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)
I was using Powershell and then I tried java -classpath .\PackageCheck;. test.Test in cmd and it worked
Then why javac -classpath .\PackageCheck -d .\ Test.java worked? I am overriding the default with -classpath but still it worked.
javac does not need to lookup your Test class - actually it can not since it is the one which is just getting compiled. javac requires the -classpath argument to resolve any other classes which the one which is just getting compiled depends on.
0

Try the following

javac -d PackageCheck Test.java

and after compiling

java -cp PackageCheck test.Test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.