NullSet
NullSet

Reputation: 113

How do I create a Makefile that will compile and run java code with command line arguments?

I need to be able to compile my program, then execute it 3 different times with a different .txt file as the first command line argument each time, and this all needs to be done with a single "make" command. The respective terminal commands for what I want my Makefile to do 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's what I currently 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 *~

Upvotes: 5

Views: 8089

Answers (1)

Tuxdude
Tuxdude

Reputation: 49473

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

Upvotes: 2

Related Questions