4

I create one makefile in my project.

JFLAGS = -g
JC = javac
JVM= $(JAVA_HOME)/bin/java
.SUFFIXES: .java .class
.java.class: ; $(JC) $(JFLAGS) $*.java

 CLASSES = \
    Class1.java \
    Class2.java \
    Main.java

 MAIN = Main

 default: classes

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

 run : classes $(JVM) $(MAIN).class

The codes below running and compile my java class but don't execute my run command. finalize the make file and don't execute my run.. Why is doing wrong in my code?

2
  • Dont waste your time trying to build Java projects with make. Make does no good job here - once you'll organize your classes in packages, you're screwed (you'll never get the dependencies right). Do yourself a favor and learn the basics of either ant or maven Commented Sep 18, 2013 at 18:38
  • I understand you point, but i need this to learned how works. Thanks for reply. Commented Sep 18, 2013 at 18:44

2 Answers 2

3

You need to add the "run" command as an action of the "run" target, rather than a prerequisite:

run : classes
        $(JVM) $(MAIN)
#make sure the line above begins with a tab
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, now work, i run using "make run" then execute my main class.
0

Make will only try to build the first target in your makefile (and all prerequisites of that target, and all prerequisites of those targets, etc.) To choose a different, or more than one, top-level target (or "goal target") you can specify them on the command line: make run.

You can move the run target to be the first target in the makefile, then it will be run by default.

Or, if you have more than one "top-level" target that you want to run, you can collect them in another rule; for example in the above you could use:

default: classes run

Although by long-standing convention this target is named all (but that's just a convention).

1 Comment

thanks for reply, your answer show me more about a makefiles and i keep trying to solve my issue. c ya.

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.