5 Replies Latest reply: Sep 9, 2007 7:31 AM by 807600 RSS

    Compilation - do I need to delete source files ?!

    807600
      Hi,

      A very simple example:

      In current directory ~home/Desktop/A.java
      package com.one;
      
      public class A {
           public static void main(String[] args) {
                boolean assertions=false;
                assert assertions=true;
                System.out.println("com.one.A:"+assertions);
           }
      }
      In current directory ~home/Desktop/B.java
      package com.two;
      
      public class B {
           public static void main(String[] args) {
                boolean assertions=false;
                assert assertions=true;
                System.out.println("com.two.B:"+assertions);
           }
      }
      In current directory ~home/Desktop/Bee.java
      import com.one.*;
      import com.two.*;
      
      
      public class Bee {
      
      
           public static void main(String[] args) {
           
                boolean assertions=false;
                assert assertions=true;
                System.out.println("Bee:"+assertions);
                A.main(null);
                B.main(null);
           
         }
      }
      Then I compile files A.java and B.java:

      javac -d . A.java B.java

      so new directory structure has been created in current ~home/Dekstop directory.

      Now when I want to compile Bee.java, I got this error:

      javac Bee.java

      Bee.java:13: cannot access A
      bad class file: ./A.java
      file does not contain class A
      Please remove or make sure it appears in the correct subdirectory of the classpath.
      A.main(null);
      ^
      1 error

      When I delete source files from current directory ~home/Desktop it compiles fine !

      Could anybody explain that to me please ?

      Thanks,

      Adrian
        • 1. Re: Compilation - do I need to delete source files ?!
          DarrylBurke
          Your source files are in the wrong folders. Read
          http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html

          db
          • 2. Re: Compilation - do I need to delete source files ?!
            796365
            Learn about using packages - read this:
            http://www.jarticles.com/package/package_eng.html
            • 3. Re: Compilation - do I need to delete source files ?!
              807600
              Hi guys,

              Thanks for your help.
              I still don't understand something here though ... :/

              The class files are put in corresponding directories. So when I compile
              Bee, why compiler doesn't use class files provided (it can find it) and why
              the source files need to be put in corresponding directories as well ?

              Thanks for your patience !


              Cheers,

              Adrian
              • 4. Re: Compilation - do I need to delete source files ?!
                807600
                So when I compile
                Bee, why compiler doesn't use class files provided
                Good question.

                In your OP you said that Bee.java compiles fine if the other two source files are deleted. What javac does is documented here http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html in the section "Searching For Types". My interpretation of that section is that the compiler would find the class files, but not the source files (because they are misplaced) and so would use the class files. But maybe that's wrong.

                Anyway, I don't have that problem because I "arrange source files in a directory tree that reflects their package tree" as the documentation suggests.
                • 5. Re: Compilation - do I need to delete source files ?!
                  807600
                  That's right.

                  I've read it. And in my case, everything should satisfy the compiler.
                  The source files and the class files are in the current directory
                  (class files in the corresponding subdirectories of the packages).
                  So when compiler wants to compiler Bee.java, it has in the current
                  directory both ./com/one/A.class and ./com/two/B.class classes
                  AND the source files if it needs to recompile it.

                  I also found this:

                  http://www.artima.com/forums/flat.jsp?forum=106&thread=14726&start=0&msRange=15
                  http://javaswamy.blogspot.com/2003/10/directories-are-java-source-file.html

                  It doesn't seem to me logic :/


                  Cheers,

                  Adrian