从具有循环依赖关系的命令行进行编译
想象一下我有两个课程(如下所示)。现在想象一下我正在从命令行使用 javac.exe 编译它们。它们不会编译,因为 A 类需要 B 类的方法存在,反之亦然。有什么技巧可以让它们从命令行编译吗? (Eclipse 可以毫无问题地编译它!)
我应该补充一下,它们当前位于两个单独的 .java 文件中。
public class A {
public void doAWork() { /* A work goes here. */}
public void doBWork() { new B().doBWork(); }
}
public class B {
public void doBWork() { /* B work goes here. */}
public void doAWork() { new A().doAWork(); }
}
Imagine that I have two classes (shown below). Now imagine that I am compiling them using javac.exe from the command line. They won't compile because class A needs class B's methods to exist and vice versa. Is there any trick to getting them to compile from the command line? (Eclipse can compile this no problems!)
I should add they are both currently in two separate .java files.
public class A {
public void doAWork() { /* A work goes here. */}
public void doBWork() { new B().doBWork(); }
}
public class B {
public void doBWork() { /* B work goes here. */}
public void doAWork() { new A().doAWork(); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您的问题在其他地方。
我可以使用以下命令完美编译 Java 1.5、1.6 和 1.7 中的代码:
即使提供单个文件名也可以完美地工作,因为 B.java 位于同一目录中:
您确定这两个文件放置在适当的目录中吗?
It looks like your issue is elsewhere.
I can perfectly compile the code in Java 1.5, 1.6 and 1.7 with the following command:
Even providing a single file name works perfectly, since B.java is in the same directory:
Are you sure the two files are placed in appropriate directories?