Home > Software design >  Java makefile with command line arguments
Java makefile with command line arguments

Time:11-11

I have a program that I wish to run and this is how one normally runs it using the command line:

java AvlTree.java inputs.txt

I wish to create a makefile for executing the above command such that it takes the command line argument which is the file_name: inputs.txt

This is the following makefile I have written:

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

CLASSES = \
        AVLTree.java

default: classes

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

clean:
    $(RM) *.class

I do not know how to tell the makefile to use the command line argument inputs.txt. Moreover, if I type make in the command line, will it execute my program also? Or will it create an executable?

I have placed the source code: AvlTree.java, inputs.txt, and the makefile in the same folder. Any help will be appreciated.

CodePudding user response:

This command

java AvlTree.java inputs.txt

is not how one normally runs a java program. It is possible to run a java program that way. But it is not "normal". Because normally you compile your java program. Which is the step you are currently doing.

javac AvlTree.java
java -cp . AvlTree inputs.txt

The second command would execute a compiled AvlTree.class. Also, you would normally not use make or a Makefile for Java. You would typically use maven, or ant, or sbt, or gradle. Not make. Anyway, you could change your Makefile like

default: run

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

run: classes
    java -cp AvlTree inputs.txt
  • Related