javac : Compiling a .java file which uses other classes in it

后端 未结 2 685
梦谈多话
梦谈多话 2020-11-28 12:43

HI i have 3 java files

a.java  
b.java  
c.java  

I managed to generate .class files for both a and b using

javac example/a         


        
相关标签:
2条回答
  • 2020-11-28 13:31

    You have to have classes a and b in your classpath when you try to compile class c. This allows the compiler to verify that they exist, figure out what methods they have, etc.

    javac is pretty sensitive to package names and classpaths. The easiest thing to do is to compile all three at the same time like so javac example/a.java example/b.java example/c.java.

    If you go to the parent directory of example (let's call it src), then you can run the following:

    javac -cp src src/example/c.java
    

    The reason you have to do it this way is because your classes have their packages listed as example. Because of your package name, javac is looking for the example directory in its classpath, where it expects to find a.class and b.class.

    0 讨论(0)
  • 2020-11-28 13:43

    Presumably you're not in the example/ directory when you run javac. Try

    javac -cp example c.java
    

    Or just cd into that directory. The classpath is not automatically resolved for the classes c.java depends on.

    0 讨论(0)
提交回复
热议问题