views:

142

answers:

5

I have a problem with a makefile. I have three classes. They do pretty simple stuff. One does an addition on a subtraction, and the last one will instantiate the two and simply print the resulted addition and subtraction.

Now when I create my makefile, I compile my Plus.java and my Minus.java but don't know how to compile the main class because it depends on the previous two. I want to compile and run it from the makefile if it's possible.

I get the above results when I try to run make:

javac -g Plus.class Minus.class
javac: invalid flag: Plus.class
Usage: javac <options> <source files>
use -help for a list of possible options
make: *** [Operation.class] Error 2

I don't know how to proceed; please forgive me if my question is to simple but I am new working with these stuff. I've searched many sites but with no answer.

A: 

you need to run javac -g Plus.java Minus.java (not .class)

an0nym0usc0ward
I don't think I understand. I show you my makefile instead maybe I cand find out this way:JCC = javacJCR = javaJFLAGS = -gdefault: Operation.classPlus.class: Plus.java $(JCC) $(JFLAGS) Plus.javaMinus.class: Minus.java $(JCC) $(JFLAGS) Minus.javaOperation.class: Operation.class $(JCC) $(JFLAGS) Operation.class run: Operation.class $(JCR) Operation.class clean: $(RM) *.class
mistique
+2  A: 

You need to pass the names of source files to javac:

javac -g Plus.java

A better option is possibly:

javac -g *.java

Very few people in the Java world invoke javac directly or use makefiles. Most people use an IDE when developing:

Eclipse: http://www.eclipse.org
Netbeans: http://www.netbeans.org

And then a more comprehensive build tool for final / automated builds:

Ant: http://ant.apache.org/
Maven: http://maven.apache.org/

Both of those could be invoked from make, or indeed invoke make themselves, in order to integrate with your existing build system.

EDIT: It seems you have a makefile issue. See if this works:

JCC = javac
JCR = java
JFLAGS = -g

all:
[TAB]$(JCC) $(JFLAGS) *.java

run:
[TAB]$(JCR) Operation

clean:
[TAB]$(RM) *.class

Replace [TAB] with an actual tab character - as you probably know this is incredibly important in make!

Russ Hayward
Thats the thing. I am currently working on a project that I do have to us these things and not the IDE's.
mistique
Thank you but the tab is there. It just didn't show up in the post
mistique
+1 for `ant`, although using `make` this way can be instructive.
trashgod
A: 

Sorry

I don't think I understand. I show you my makefile instead maybe I cand find out this way:

JCC = javac

JCR = java

JFLAGS = -g

default: Operation.class

Plus.class: Plus.java
    $(JCC) $(JFLAGS) Plus.java

Minus.class: Minus.java
    $(JCC) $(JFLAGS) Minus.java

Operation.class: Operation.class
    $(JCC) $(JFLAGS) Operation.class  

run: Operation.class
    $(JCR) Operation.class

clean:
    $(RM) *.class
mistique
Reformatted code; please revert if incorrect. Rather than creating a new answer, you can edit your question to include code such as this.
trashgod
A: 

Try this. Alas, managing Java class dependencies is a pain using make (been there done that in relatively large projects), so you should still seriously consider using Ant or some other appropriate tool.

JCC = javac

JCR = java

JFLAGS = -g

default: Operation.class

Plus.class: Plus.java
            $(JCC) $(JFLAGS) Plus.java

Minus.class: Minus.java
             $(JCC) $(JFLAGS) Minus.java

Operation.class: Operation.java Plus.class Minus.class
                $(JCC) $(JFLAGS) Operation.java 

run: Operation.class
    $(JCR) Operation.class

clean: $(RM) *.class
Mart Oruaas
A: 

I solved it. It was a problem with the packages. I edited my files in netbeans and remained:

package operations

to every one of them

so I got Operation to compile but now it didn't run

Silly problem....

JCC = javac

JCR = java

JFLAGS = -g

default: Operation.class Plus.class Minus.class

Plus.class: Plus.java $(JCC) $(JFLAGS) Plus.java

Minus.class: Minus.java $(JCC) $(JFLAGS) Minus.java

Operation.class: Operation.java Plus.java Minus.java $(JCC) $(JFLAGS) Operation.java Plus.java Minus.java

run: $(JCR) Operation

clean: $(RM) *.class

mistique
@mistique: Try formatting your `Makefile` as code using the `101` button.
trashgod