smatter
smatter

Reputation: 29178

Makefile for java

I am running Eclipse in windows for doing my homeworks. I have to submit a makefile so that my code (singlefile.java) can be compiled. How can I do this.

I found this. Can it be tested on windows.

EDIT: As per the suggestions, I installed gnu make for windows:

My make file is as follows:

JFLAGS = -g
JC = javac
#
.SUFFIXES: .java .class
.java.class:
        $(JC) $(JFLAGS) $*.java
CLASSES = \
        singlefile.java \
default: classes
classes: $(CLASSES:.java=.class)
clean:
        $(RM) *.class

However, when I run make, I am getting the error:

*** missing separator (did you mean TAB instead of 8 spaces?).  Stop.

I am new to make. What am I missing?

Upvotes: 1

Views: 19082

Answers (7)

rflood89
rflood89

Reputation: 714

Make insists that you use tabs instead of spaces. The error message even tells you that "(did you mean TAB instead of 8 spaces?)". So go to the lines where the code is indented, press backspace 8 times, then press tab. Do this on all lines and it will work.

Upvotes: 4

Robert H
Robert H

Reputation: 1663

download "managing projects with GNU make" from http://www.wanderinghorse.net/computing/make/ by Stephan Beal and original edition by Robert Mecklenburg, and look at page 121. It has everything you need to build Java using gnumake

Upvotes: 0

Michał Niklas
Michał Niklas

Reputation: 54302

All you have to do with this makefile is to change Purchaser.java into your .java source and remove other .java sources from it. If you use Windows, then you probably do not have make installed in your system. You can install and use GNU make, Microsoft nmake or other. I think the simplest way to compile your program is to make .bat file with content like:

javac MyClacc.java

Of course you can add there javac flags like -deprecation etc.

Simplest form of makefile in your case can look like:

default:
    javac singlefile.java

clean:
    del singlefile.class

Be sure to make indentation (before commands like javac and del) with tab character.

Upvotes: 4

Stephen C
Stephen C

Reputation: 718798

The Makefile you linked is a suitable starting point.

Can it be tested on windows?

Only if you have installed Cygwin or something like that. Even then, there is a small risk that you will create a makefile that contains Windows platform dependencies ...

I'd recommend testing the makefile on the platform that they provide for you to do your software development on, which is most likely some flavour of UNIX ... given that they expect you to supply a makefile.

But as others have said, if you have the option, use Ant instead of Make for building simple Java projects. Ant has the advantage of being more platform independent, and requiring little apart from an Ant installation and a Java installation.

Upvotes: 1

Catchwa
Catchwa

Reputation: 5855

Because you're using Eclipse and Java you should look at creating an Ant buildfile.

Upvotes: 2

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

If you don't have any dependencies, you can write a makefile just as you would with C programs with something like:

JAVAC = javac

%.class: %.java
    $(JAVAC) $<

(note: you'd probably want a clean task as well)

If you're planning on continuing with java for more advanced projects, however, consider using maven or ant for building your project. Both can be integrated into eclipse so you don't have to do much work. Then you can wrap your 'mvn compile' command into a Makefile and forget about it.

Upvotes: 4

MeBigFatGuy
MeBigFatGuy

Reputation: 28568

do you have to use make? or can you use any build tool. Make is rarely if ever used for java, usually ant.apache.org or maven.apache.org, the first being simpler.

Upvotes: 1

Related Questions