The situation is as follows. jdk1.6.0_25 In the directory ./test/p program Cl1.java In the CLASSPATH, the path to ./test/p is registered. CLASSPATH =; C: \ Documents and Settings \ avp \ java \ src

package test.p; public class Cl1 { public Cl1() { System.out.println("Cl1 ()"); } public static void priTest() { System.out.println("Cl1 test"); } public static void main (String [] av) { System.out.println("Cli main test"); } } 

And in the directory ./test program Tcl1.java

 import test.p.*; public class Tcl1 { public static void main (String [] av) { Cl1 c = new Cl1(); c.priTest(); } } 

Execute commands

 c:/Documents and Settings/avp/java/src/test/p $ javac Cl1.java c:/Documents and Settings/avp/java/src/test/p $ cd .. c:/Documents and Settings/avp/java/src/test $ javac Tcl1.java c:/Documents and Settings/avp/java/src/test $ java Tcl1 Cl1 () Cl1 test c:/Documents and Settings/avp/java/src/test $ c:/Documents and Settings/avp/java/src/test $ 

Bye ok

Copy Tcl1.java to ./test/p and try to compile it. Question number 1

 c:/Documents and Settings/avp/java/src/test $ cp Tcl1.java p/ c:/Documents and Settings/avp/java/src/test $ cd p c:/Documents and Settings/avp/java/src/test/p $ javac Tcl1.java Tcl1.java:7: cannot access Cl1 bad class file: .\Cl1.class class file contains wrong class: test.p.Cl1 Please remove or make sure it appears in the correct subdirectory of the classpath. Cl1 c = new Cl1(); ^ 1 error c:/Documents and Settings/avp/java/src/test/p $ c:/Documents and Settings/avp/java/src/test/p $ 

Attempt to perform Cl1.class (after all, it has main ()!) Question number 2

 c:/Documents and Settings/avp/java/src/test/p $ java Cl1 java.lang.NoClassDefFoundError: Cl1 (wrong name: test/p/Cl1) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) ......... at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: Cl1. Program will exit. Exception in thread "main" c:/Documents and Settings/avp/java/src/test/p $ 

Naturally, if you comment out the line with the package in Cl1.java, then everything compiles and Cl1 works.

Sorry for the long and probably stupid question, but I read the books (apparently not carefully enough), but I don’t understand why the package and main are incompatible , and why the class with main is not compiled in the same table of contents where the imported classes lie. (However, compiled elsewhere, it runs anywhere).
Or am I doing something elementary wrong?

In the command window of Windows, everything is exactly the same, but I cannot copy-paste from there.

    1 answer 1

    Compilation with question number 1 does not work because CLASSPATH contains a class that is not in the same path as its package. Namely: Cl1 belongs to the test.p package, i.e. Must be in the test/p directory relative to CLASSPATH ; but when searching for classes, the compiler walks around the paths to CLASSPATH sequentially - and in this case, first checks the current directory ./ , finds some wrong location (the package is declared like test.p , and lies stupidly without the package) class and curses. Try to delete ./ from CLASSPATH at compile time (or go up 2 levels) - and everything will be ok.

    Running with question number 2 does not work because you specify an unqualified class name - this is not Cl1 , but test.p.Cl1 .

    • Thank ! About 1) I understood perfectly. But still without. in CLASSPATH not working. Already for the reason that it cannot find (when running java Tcl1) a class with main. java test \\ Tcl1 is the same - I can’t find test \\ Tcl1. Although Tcl1.class is there. Ie still did not work . About 2). I was thinking of using this technique (main in class) for the purpose of local class testing. But, it turns out, without changes in the text it will not work? - avp
    • 1) yes, it does not start, because the current directory has been deleted from CLASSPATH . By the way, it is Tcl1 to access Tcl1 through test.Tcl1 just because it lies in test/ - this package is not specified in the class declaration. 2) JUnit is usually used for testing. In general, these frauds with the movement of classes and the strange arrangement of packages are not quite clear (somehow test.p is a package for Cl1, and test for Tcl1 is not a package). Are you just learning how it works, or want to achieve something? - yozh
    • Exactly, I'm just learning how this thing behaves, which the Silver Bullets castors have done for American Vacuum Cleaner Manufacturers. And then she (Java) began to live her life in new niches. - avp