Compiling all modified Java files in a folder on Unix

Hi all,

I have a Unix script that will compile all Java files in a sub folder as follows:

find . -name "*.java" -print -exec $JAVA_HOME/bin/javac -cp .:$CLASSPATH '{}' \;

I would like to enhance it to only compile those Java files who:

1.) Have no class file
2.) Have a class file modified in the past.

Could someone please assist me with this? Thanks for any help.

The make tool is designed for this. When you run 'make', it reads Makefile from the current directory to see if it needs to build anything.

Here's a simple Makefile that creates output from file1, file2, file3, file4:

output:file1 file2 file3 file4
        cat file1 file2 file3 file4 > $@

Note that the eight spaces in front of the cat command are actually one tab and must be a tab for make to understand the file.

The first line tells it to make output from the files listed after :. I think wildcards are allowed, so you could put that as file* instead of file1 file2 file3 file4. When you run make, it will check if any of the input files are newer than output and re-create it if necessary. If none of those files are newer than output, it will do nothing.

You can specify as many rules as you want, in any order, but the first rule is the one it actually attempts to build, so, you'd usually have an empty rule like:

all:output1 output2 output3 output4

at the top, which will cause make to check the rules to build output1, output2, output3, output4, etc.