Question

I am taking a java programming class right now and it is my first real programming experience (although i have done some programming before). for the class i have to use jEdit to write programs, and terminal to compile and run them (i have a mac, and programs that automatically compile, run, and debug programs are banned for educational purposes). things were going very well for me at first, but a few days ago, terminal stopped overwriting the .class files on my QuickSort program when i try to compile it. i change the code so that it will look completely different when it runs, but after i compile and run it, there is no change. however, if i delete the .class files before i recompile, the changes are implemented next time i run it. neither me nor my teacher have been able to find any info on this problem or how to fix it (the exact same thing did happen to a classmate of mine three days before it happened to me, and we are the only mac users in the class). if anyone knows what the problem is, how to fix it, or where i can find someone who does know how to fix it, i would really appreciate it. deleting five .class files every time i compile my program is a huge pain and has already led to some serious problems for me. thank you so much for all of your help!

*edit: i am running mac os x 10.8.5

Was it helpful?

Solution

The compiler isn't smart enough about detecting which class files are out of date, so either manually tell it to recompile everything using javac *.java or use a build tool such as Ant.


The reason the compiler doesn't recompile all of the classes is that it tries to be 'smart' and avoid unnecessary work by only recompiling files that changed. If a .class file is newer than the matching .java file, the source code hasn't changed since the previous compilation so the class isn't recompiled.

This can cause problems when there are multiple files with a chain of dependencies. Consider this simple example:

// file: A.java
public class A { public static void main(String[] args) { new B().run(); } }

// file: B.java
public class B { void run() { new C().run(); } }

// file: C.java
public class C { void run() { System.out.println("hello world"); } }

When A is compiled for the first time, the compiler sees that it references B, which in turn references C. All three are compiled and all is well.

If B.java is modified and A is recompiled, the compiler sees that A references B and since B.java is newer than B.class it is recompiled. It doesn't recompile C because C.java hasn't changed. All is still well.

But if C.java is modified and A is recompiled the compiler sees that A depends on B, but since B.java hasn't changed it is not recompiled. So the compiler never gets to C and doesn't recompile it even though C.java has changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top