How to create a Makefile that will compile and run Java code with command line arguments?

I need to compile my program and then execute it 3 times with another .txt file as the first command line argument every time, and all this needs to be done with a single make command. The corresponding terminal commands for what I want to do the Makefile are as follows:

javac MainDriver.java FSA.java State.java Transition.java
java MainDriver test1.txt
java MainDriver test2.txt
java MainDriver test3.txt

Here I have:

JC = javac
JCR = java

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

CLASSES = \
    MainDriver.java \
    FSA.java \
    State.java \
    Transition.java 

default: classes

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

clean:
    $(RM) *.class *~
+5
source share
1 answer
JC = javac
JCR = java

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

CLASSES = \
    MainDriver.java \
    FSA.java \
    State.java \
    Transition.java 

TXT_FILES = \
    test1.txt \
    test2.txt \
    test3.txt \

default: classes exec-tests

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

clean:
    $(RM) *.class *~

exec-tests: classes
    set -e; \
    for file in $(TXT_FILES); do $(JCR) MainDriver $$file; done;


.PHONY: default clean classes exec-tests
+2
source

All Articles