Home » Java » Makefile with Java-Exceptionshub

Makefile with Java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I realize the make isn’t the best tool to be using with Java, but I just wanted to experiment with it. I have this script but I’m not sure why it isn’t working:

JFLAGS = -g
JC = javac
SRC_DIR = $(PWD)
.SUFFIXES:  .java .class
.java.class: 
    $(JC) $(JFLAGS) $*.java

CLASSES = \
$(SRC_DIR)/Fibonacci_Methods.java \
$(SRC_DIR)/Fibonacci_Methods_Test.java \

default: classes

classes: 
    $(CLASSES:.java=.class)

clean:
    $(RM) *.class

I get this error:

/path/to/make_test/Fibonacci_Methods.class 
/path/to/make_test/Fibonacci_Methods_Test.class
make: /path/to/make_test/Fibonacci_Methods.class: No such file or directory
make: *** [classes] Error 1

I’m not sure why. My understanding is that this script should define CLASSES, which should call .java.class target because I’m defining files ending with .java. I don’t know the purpose of $(CLASSES:.java=.class) because I would have thought the compilation be done already before this step.

I have not yet compiled the java code, by the way (so I’m running make with .java files only, if that makes a difference).

How to&Answers:

This is wrong:

classes: 
        $(CLASSES:.java=.class)

Here you’ve defined a target with no prerequisites and recipe (which is supposed to be a command that is used to rebuild the target) that consists of a list of .class filenames, so you’re getting the error you see because you can’t “run” a list of .class files.

You want this:

classes: 
        $(CLASSES:.java=.class)

which defines a target classes with a set of prerequisites which are the .class files you want to build, and no recipe because you don’t want to create a target named classes.