File: A.java
class A
{
B b;
public A() {
b = new B();
}
}
File: B.java
class B
{
public B() {}
public foo(A a) {...}
}
The above code can not be compiled because A needs B before it can compile and B needs A before it can compile. Neither shall compile before the other. Now what?
This example is trivial. I could remove foo(A a) {...} such B.java would compile. Then compile A.java. Restore B.java then compile it. But I’m trying to build RXTX from source and its dependencies are a maze of twisty little phrases.
I had hoped that I could compile to non-working class code. Then with all the classes and methods defined compile again into working code.
Is there a magic bullet?
They should compile fine if you compile them both at once:
javac A.java B.java
There is no compilation problem, if you use a Java IDE (Eclipse, or Netbeans etc) or ant build script etc (the major thing is that it should be right classpath when using javac command).
or if you compile from the command line with *.java, javac is smarter than this ;)
javac -classpath ... *.java
The Java Compiler works from your classpath, this is rule no 1 in Java!
If you hava A and B source code, and you do a compilation with the right classpath then there is no problem with the javac task.
If you just do a compilation of your first class (A or B), the Java compiler will search the classpath for the requested classes (as specified in your import, if no import package is given, then the classpath should be as the selected class).
If A.java and B.java are in the same package, and you do run a javac -classpath . A.java this should result in a A.class and B.class on the fly.
If A.java is in package x and B in package y, you should of course use both classpaths during compilation. javac -classpath x;y;.; A.java (this will compile even B.java if it is not already compiled!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With