Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 *~
like image 540
soccercta100 Avatar asked Nov 03 '22 03:11

soccercta100


1 Answers

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
like image 95
Tuxdude Avatar answered Nov 09 '22 08:11

Tuxdude