Java – makefile for java

javamakefile

i don't understand what i did wrong with my makefile :

JAVA_SRCS:=$(wildcard tasks/src/*.java)
JAVA_CLASSES=$(subst /src/,/build/,$(JAVA_SRCS:.java=.class))
JFLAGS=-cp jar/octobot.jar -d tasks/build
JC=javac

.SUFFIXES: .java .class

.java.class:
 $(JC) $(JFLAGS) $*.java

default: build

build: $(JAVA_CLASSES)

clean:
 $(RM) tasks/build/*.class

I got this error :

make: *** No rule to make target `tasks/build/ClickTask.class', needed by `classes'.  Stop.
zsh: exit 2     make

But strangely, when i re-write the rule build like this :

build: $(JAVA_SRCS:.java=.class)

no error, the rule is launched but does it every time (and it's not correct)

Best Answer

@Dean Povey is correct: you can't do this with suffix rules, because they look in the same directory as the source. You can, however, do this with a GNU Make pattern rule (and you're already using GNUMake-isms in your Makefile, so whatever):

tasks/build/%.class: tasks/src/%.java
        $(JC) $(JFLAGS) $<

Note, however, that make is ill-suited to building java source as one .java file can result in many .class files (inner classes, for instance). Automake's approach to this problem is to compile everything in a single call to javac and write out a timestamp file (echo timestamp > classnoinst.stamp, for example). Then anything that needs the java sources built depends on that stamp file and make clean removes the .stamp along with the .class files.