views:

140

answers:

3

I have a large number of classes in a project, and I would like to compile all of them from a script. The thing is, the classes should be compiled in a certain order, for example: I have a class named A which depends on a class named B. Let's say class B depends on a class named C. In order for me to compile class A, I would have to compile first B and C.

Is there some tool I could use to establish the compile order of the classes, so that I don't have to parse each class and determine this myself? I would preffer that the tool can save a file with the order of the files to be compiled, so that I could parse that from my script.

Thanks !

+6  A: 

If you compile all of them at the same time (in the same javac invocation), you do not need to do anything of the sort you are describing. javac is smart enough to compile all files you give to it at the same time, so it doesn't have any problem with out-of-order compilation.

Michael Myers
i'll use this for now, and when I have some free time, I'll try to implement this myself :)
Geo
+2  A: 

The Java compiler (Javac) already builds up a dependency list of all the class files you need to compile. The real dependency here is between packages - not individual java files in the same package (this is automatically taken care of by the compiler).

Use a tool like Ant or Maven to specify and compile all the files in various packages and produce your final distribution.

If you are using an IDE like NetBeans, it automatically does this for you. Alternatively, if use a tool like JDepend

Dinuk
i would like to be able to perform the compilation myself.
Geo
Okay - well, if you wrote all the code yourself, and organized them in the meaningful packages, you may know what the dependencies between each of the packages are :)If you are attempting this as an academic exercise, then the easiest way to learn how to achieve this is to look at how Ant uses the build.xml file to work out what the dependencies are.When you say 'large' amount of java files, how many are we talking about here? and how many packages?
Dinuk
it's an exercise, the classes are automatically generated by an other script of mine, so I can configure the number of classes to be generated. I know I could create the "graph" when I'm generating the code for the classes, but I would like this to be able to work on classes that aren't generated by me :)
Geo
Have you tried something like JDepend ? (http://clarkware.com/software/JDepend.html)
Dinuk
+1  A: 

These kinds of DAG ordering problems are usually solved with topological sorting. See Wikipedia for a description. I don't know if there is a tool such as the one you are looking for, but implementing it yourself is should not be that difficult.

Georgios Gousios