10 Replies Latest reply: Mar 4, 2008 9:19 PM by 807601 RSS

    .java files located in different directories - trouble compiling

    807601
      My attempts at getting my main .class file (../master/BicycleMaster.class) to automatically recognize that it's supporting .class file (../master/child/BicycleChild.class) located one directory below keep failing and the .java files won't compile.

      If I move both .java files to the same location (in the ../master directory) then they compile and run just fine, but it's when I want to separate them into the two different directories that I have a problem compiling them. They won't compile because the references in BicycleMaster.jave to BicycleChild.java can't be resolved.

      I was hoping - if you have time - that you might be able to show me by example in the code below? In the file ../master/BicycleMaster.java I added 'package master;' as the first line of the code. It was my impression that this would tell the compiler to look in the sub-directories for any additional .java files. This didn't work either.

      HERE ARE THE DETAILS:

      Directory of K:\COMMON\ITS\STEVEB\java\master

      03/04/2008 04:25 PM <DIR> .
      03/04/2008 04:25 PM <DIR> ..
      03/04/2008 04:24 PM 612 BicycleMaster.java
      03/04/2008 03:54 PM <DIR> child
      1 File(s) 612 bytes



      K:\COMMON\ITS\STEVEB\java\master>type BicycleMaster.java
      package master;
      class BicycleMaster {
          public static void main(String[] args) {
              //Create 2 different bicycle objects
              BicycleChild bike1 = new BicycleChild();
              BicycleChild bike2 = new BicycleChild();
              //Invoke methods on the objects
              bike1.changeCadence(50);
              bike1.speedUp(10);
              bike1.changeGear(2);
              bike1.printStates();
      
              bike2.changeCadence(50);
              bike2.speedUp(10);
              bike2.changeGear(2);
              bike2.changeCadence(40);
              bike2.speedUp(10);
              bike2.changeGear(3);
              bike2.printStates();
          }
      }
      Directory of K:\COMMON\ITS\STEVEB\java\master\child

      03/04/2008 03:54 PM <DIR> .
      03/04/2008 03:54 PM <DIR> ..
      03/04/2008 03:54 PM 524 BicycleChild.java
      1 File(s) 524 bytes



      K:\COMMON\ITS\STEVEB\java\master\child>type BicycleChild.java
      class BicycleChild {
          int cadence = 0;
          int speed = 0;
          int gear = 1;
          void changeCadence(int NewValue) {
              cadence = NewValue;
          }
          void changeGear(int NewValue) {
              gear = NewValue;
          }
          void speedUp(int increment) {
              speed = speed + increment;
          }
          void applyBrakes(int decrement) {
              speed = speed - decrement;
          }
          void printStates() {
              System.out.println("Your cadence: "+cadence+", Your speed: "+speed+", Your gear: "+gear);
          }
      }
      HERE ARE THE ERRORS:

      First I compile the ../master/child/BicycleChild.java file and it compiles fine. This I attempt to compile ../master/BicycleMaster.java and I get the following errors:

      K:\COMMON\ITS\STEVEB\java\master>javac BicycleMaster.java
      BicycleMaster.java:5: cannot find symbol
      symbol : class BicycleChild
      location: class master.BicycleMaster
      BicycleChild bike1 = new BicycleChild();
      ^
      BicycleMaster.java:5: cannot find symbol
      symbol : class BicycleChild
      location: class master.BicycleMaster
      BicycleChild bike1 = new BicycleChild();
      ^
      BicycleMaster.java:6: cannot find symbol
      symbol : class BicycleChild
      location: class master.BicycleMaster
      BicycleChild bike2 = new BicycleChild();
      ^
      BicycleMaster.java:6: cannot find symbol
      symbol : class BicycleChild
      location: class master.BicycleMaster
      BicycleChild bike2 = new BicycleChild();
      ^
      4 errors
        • 1. Re: .java files located in different directories - trouble compiling
          807601
          It's all the magic of classpath :)

          Assume your directory K:\COMMON\ITS\STEVEB\java\ is the root directory for your work. Subdirectories within this should match to the package that you give for your class.
          BicycleMaster.java has the package master;, so it should reside in "master" directory within your "work root", i.e. K:\COMMON\ITS\STEVEB\java\master.
          You have put BicycleChild.java under K:\COMMON\ITS\STEVEB\java\master\child directory, but you haven't specified any package to it. So specify package master.child; to BicycleChild.java.

          Now compile the classes like this:
          K:\COMMON\ITS\STEVEB\java\:>set CLASSPATH=.:K:\COMMON\ITS\STEVEB\java\:%CLASSPATH%
          K:\COMMON\ITS\STEVEB\java\:>cd master\child
          K:\COMMON\ITS\STEVEB\java\master\child\:>javac BicycleChild.java
          K:\COMMON\ITS\STEVEB\java\master\child\:>cd ..
          K:\COMMON\ITS\STEVEB\java\master\:>javac BicycleMaster.java

          This should work. The trick is, you have specified your "work-root" directory (K:\COMMON\ITS\STEVEB\java\:>) in the classpath. Any files under this should follow the package structure you have defined for your classes.
          • 2. Re: .java files located in different directories - trouble compiling
            807601
            Thanks so much for responding. Progress was definetly made as the compiler now works longer before erroring out. Sadly it's still erroring out for a reason I'm not understanding. Please let me know if you can see where I'm going wrong.

            Here's how it played out for me:

            K:\COMMON\ITS\STEVEB\java>echo %classpath%
            .;k:\common\its\steveb\java\;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip

            K:\COMMON\ITS\STEVEB\java\master\child>javac BicycleChild.java

            K:\COMMON\ITS\STEVEB\java\master\child>cd ..

            K:\COMMON\ITS\STEVEB\java\master>javac BicycleMaster.java
            BicycleMaster.java:5: cannot find symbol
            symbol : class BicycleChild
            location: class master.BicycleMaster
            BicycleChild bike1 = new BicycleChild();
            ^
            BicycleMaster.java:5: cannot find symbol
            symbol : class BicycleChild
            location: class master.BicycleMaster
            BicycleChild bike1 = new BicycleChild();
            ^
            BicycleMaster.java:6: cannot find symbol
            symbol : class BicycleChild
            location: class master.BicycleMaster
            BicycleChild bike2 = new BicycleChild();
            ^
            BicycleMaster.java:6: cannot find symbol
            symbol : class BicycleChild
            location: class master.BicycleMaster
            BicycleChild bike2 = new BicycleChild();
            ^
            4 errors



            Here are the two programs revised as you suggested:

            K:\COMMON\ITS\STEVEB\java\master\child>type BicycleChild.java
            package master.child;
            class BicycleChild {
                int cadence = 0;
                int speed = 0;
                int gear = 1;
                void changeCadence(int NewValue) {
                    cadence = NewValue;
                }
                void changeGear(int NewValue) {
                    gear = NewValue;
                }
                void speedUp(int increment) {
                    speed = speed + increment;
                }
                void applyBrakes(int decrement) {
                    speed = speed - decrement;
                }
                void printStates() {
                    System.out.println("Your cadence: "+cadence+", Your speed: "+speed+", Your gear: "+gear);
                }
            }
            K:\COMMON\ITS\STEVEB\java\master>type BicycleMaster.java
            package master;
            class BicycleMaster {
                public static void main(String[] args) {
                    //Create 2 different bicycle objects
                    BicycleChild bike1 = new BicycleChild();
                    BicycleChild bike2 = new BicycleChild();
                    //Invoke methods on the objects
                    bike1.changeCadence(50);
                    bike1.speedUp(10);
                    bike1.changeGear(2);
                    bike1.printStates();
            
                    bike2.changeCadence(50);
                    bike2.speedUp(10);
                    bike2.changeGear(2);
                    bike2.changeCadence(40);
                    bike2.speedUp(10);
                    bike2.changeGear(3);
                    bike2.printStates();
                }
            }
            • 3. Re: .java files located in different directories - trouble compiling
              800645
              import the child package in BicycleMaster

              import master.child.BicycleChild;

              It should work
              • 4. Re: .java files located in different directories - trouble compiling
                807601
                import master.child.BicycleChild;
                Where do I put this code in the program? Does it have to be the first line? Or should it come after the 'package' statement? Or does it matter at all?
                • 5. Re: .java files located in different directories - trouble compiling
                  800645
                  before the class definition.

                  You should put it after the package line.

                  Please take a look of the java basics.

                  http://java.sun.com/docs/books/tutorial/java/package/packages.html
                  • 6. Re: .java files located in different directories - trouble compiling
                    807601
                    Thanks for continued help! I really appreciate the help in figuring out this example. This early stage of learning is often made more frustrating when folks only point you to documentation that can, at times, be just overwhelming when you are trying something so simple.

                    Arg...I'm still getting errors, but the errors are now different so that's a great sign!
                    Here's how it played out:

                    K:\COMMON\ITS\STEVEB\java\master>echo %classpath%
                    .;K:\COMMON\ITS\STEVEB\java\;

                    K:\COMMON\ITS\STEVEB\java\master>type BicycleMaster.java
                    package master;
                    import master.child.BicycleChild;
                    class BicycleMaster {
                        public static void main(String[] args) {
                            //Create 2 different bicycle objects
                            BicycleChild bike1 = new BicycleChild();
                            BicycleChild bike2 = new BicycleChild();
                            //Invoke methods on the objects
                            bike1.changeCadence(50);
                            bike1.speedUp(10);
                            bike1.changeGear(2);
                            bike1.printStates();
                    
                            bike2.changeCadence(50);
                            bike2.speedUp(10);
                            bike2.changeGear(2);
                            bike2.changeCadence(40);
                            bike2.speedUp(10);
                            bike2.changeGear(3);
                            bike2.printStates();
                        }
                    }
                    K:\COMMON\ITS\STEVEB\java\master>javac BicycleMaster.java
                    BicycleMaster.java:2: master.child.BicycleChild is not public in master.child; cannot be accessed from outside package

                    import master.child.BicycleChild;
                    ^
                    BicycleMaster.java:6: cannot find symbol
                    symbol : class BicycleChild
                    location: class master.BicycleMaster
                    BicycleChild bike1 = new BicycleChild();
                    ^
                    BicycleMaster.java:6: cannot find symbol
                    symbol : class BicycleChild
                    location: class master.BicycleMaster
                    BicycleChild bike1 = new BicycleChild();
                    ^
                    BicycleMaster.java:7: cannot find symbol
                    symbol : class BicycleChild
                    location: class master.BicycleMaster
                    BicycleChild bike2 = new BicycleChild();
                    ^
                    BicycleMaster.java:7: cannot find symbol
                    symbol : class BicycleChild
                    location: class master.BicycleMaster
                    BicycleChild bike2 = new BicycleChild();
                    ^
                    5 errors
                    • 7. Re: .java files located in different directories - trouble compiling
                      800645
                      ok, make all your classes public.
                      public class MyClas{
                      //everything
                      }
                      • 8. Re: .java files located in different directories - trouble compiling
                        807601
                        More progress...both .java files now compiled! But then new errors....

                        Here's where I added the 'public' keyword in BicycleChild.java:

                        K:\COMMON\ITS\STEVEB\java\master\child>type BicycleChild.java
                        package master.child;
                        public class BicycleChild {
                            int cadence = 0;
                            int speed = 0;
                            int gear = 1;
                            public void changeCadence(int NewValue) {
                                cadence = NewValue;
                            }
                            public void changeGear(int NewValue) {
                                gear = NewValue;
                            }
                            public void speedUp(int increment) {
                                speed = speed + increment;
                            }
                            public void applyBrakes(int decrement) {
                                speed = speed - decrement;
                            }
                            public void printStates() {
                                System.out.println("Your cadence: "+cadence+", Your speed: "+speed+", Yo
                        ur gear: "+gear);
                            }
                        }
                        But then I attempted to run the BicycleMaster.class file and got quite a mess of errors:

                        K:\COMMON\ITS\STEVEB\java\master>java BicycleMaster
                        Exception in thread "main" java.lang.NoClassDefFoundError: BicycleMaster (wrong
                        name: master/BicycleMaster)
                        at java.lang.ClassLoader.defineClass1(Native Method)
                        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
                        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
                        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
                        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
                        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
                        at java.security.AccessController.doPrivileged(Native Method)
                        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
                        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                        • 9. Re: .java files located in different directories - trouble compiling
                          800645
                          You have to put the package name in the class as a prefix so that the compiler find your class:
                          java master.BicycleMaster
                          because the class belongs to the package.

                          regards
                          • 10. Re: .java files located in different directories - trouble compiling
                            807601
                            Perfect, that worked! I've really learned so much by going through this simple exercise. I'd have given up long ago if I didn't get the help from this post. I really, really appreciate it. Thanks.