jynx678
jynx678

Reputation: 249

Java Makefile "No rule to make target"

I am trying to write a makefile for my java program. I've tried looking online and this is what I've come up with.

JFLAGS = -g

JC = javac

JVM = java

FILE=

.SUFFIXES: .java .class

.java.class:

        $(JC) $(JFLAGS) $*.java

CLASSES = banker.java

MAIN = Main

default: classes

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

run: classes
    $(JVM) $(MAIN)

I am getting an error that says

No rule to make target 'banker.class', needed by 'classes'. Stop.

I only have one class: banker.java

Upvotes: 2

Views: 7190

Answers (1)

wizurd
wizurd

Reputation: 3739

It looks like banker.java isn't in the same directory as the Makefile.

If not, you need to instead say: "CLASSES = relative/path/to/banker.java"

So, for example, if you have a directory structure like this:

MyProject
   -jsrc
     -banker.java
   -jbin
   -Makefile

and your banker.java is in MyProject/jsrc, then you need to change

CLASSES = banker.java

To be

CLASSES = jsrc/banker.java

And, unless you want the .class files to also be in jsrc, you need to change

$(JC) $(JFLAGS) $*.java

to

$(JC) -d $(JCLASSDIR)/. $(JFLAGS) $*.java

And add

JCLASSDIR=jbin

At the top

JFLAGS = -g

JC = javac

JCLASSDIR=jbin

JVM = java

FILE=

.SUFFIXES: .java .class

.java.class:

        $(JC) -d $(JCLASSDIR)/. $(JFLAGS) $*.java

CLASSES = jsrc/banker.java

MAIN = Main

default: classes

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

run: classes
    $(JVM) $(MAIN)

Upvotes: 1

Related Questions