Build java classes having circular reference / import

I am working on some codes that involved a circular reference which gives me a headache to compile. I googled circular reference / import java, the information I found does not really help me to solve the problem. I understand it might be poor design but I am working with codes not written by me or my colleague. Changing the code requires greater effort.

I wrote a simple test code that isolates the problem. The problem is funny: if I keep class file within the same directory as its source file, it will compile. If I attempt to isolate class file from the .java file, it won't compile. Let me put up my code:

The directory I am working with is named: C:\java\Testing\testcircular.
C:\java is added to my CLASSPATH environmental variable.

I make 2 folders in testcircular called src and com. That's attempting to seperate source code from class files.

I write 2 simple test classes: called testClass1.java, and testClass2.java . I package testClass1.java into packageA, testClas2 to packageB.

(Note if working properly, the test client will print the Fibonacci sequence.

================================================== ==========

Now here is the problem: note that in make.bat, I commented out the move statement. So the .class file is left in the src folder. Accordingly, the package statement in the codes bears "package ***.src.packageA" . Now that compilation works, and the test client works fine.

HOWEVER, if I un-comment the move statement in make file, and change every occurrence of "src" into "com" in the codes, the compilation doesn't work. Could someone help to explain why and offer a solution?

Re: Build java classes having circular reference / import

First, your classes are in Testing.testcircular.src.* packages, but then you move the class files to testing/testcircular/com. No wonder the compiler can't find them there.

Say your directory structure looks like this:

Java Code:

java
testcircular
src
bin

src and bin should should both have directory structures inside them corresponding to your package names. You should only be running javac once, and when you do, src should be in your classpath and bin should not (or else javac may find classes in bin and not recompile your changes). If you simply compile the main class of your program, javac will descend into the src tree and compile everything needed, automatically resolving circular references. Then you can copy all the class files to the bin tree, and run your main class with bin in your classpath.

Also, see the link in my sig for naming conventions. Naming things the way you do makes your code hard to follow.

Re: Build java classes having circular reference / import

I see. However, the code I put up there compiles correctly. I am able to run my make file cleanly. I think the make file is essentially in the same spirit as your 2 step compilation above. because I have C:\java in my CLASSPATH env variable.

What doesn't work, is if I change every src to com. the error I receive is:

Re: Build java classes having circular reference / import

1: If you put the classes in a com.* package but they're not in the com/ directory relative to the classpath, they won't compile.

Classpath trips a lot of people up, but it's not really that complicated. Classpath is simply a directory (or a list of directories) relative to which javac and java look for classes. If your classpath is c:\java, your package name is mypackage, and a class in that package is called MyClass, then the source file must be c:\java\mypackage\MyClass.java and the class file must be c:\java\mypackage\MyClass.class.

What I'm suggesting is that you can change the classpath between compiling and running. You can make the classpath be c:\java\src when you compile, then move your class files to c:\java\bin and make that the classpath when you run the program.