0

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.

1
  • I have only passing knowledge of make, but from what I can piece together from the above, there is no call to java only to javac. Commented Nov 11, 2022 at 5:53

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ohh, now I get why I was having a hard time finding java make content since you use Gradle and Maven for it. Also, by editing the makefile, do you mean just appending the instructions from your answer to the original makefile?
@RishabParmar Almost. Replace the default target in your current Makefile as above. Then add the run target (with the classes dependency) at the bottom. And it should behave as you would like. But this is likely to be fragile. Make is an older technology.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.