WizDom
WizDom

Reputation: 57

Creating a Makefile for jar executable

I am currently working with the following Makefile:

    JAVAC=javac
    sources = $(wildcard *.java)
    classes = $(sources:.java=.class)

    all: myProgram

    myProgram: $(classes)

    clean :
        rm -f *.class

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

myProgram was orignally just a .java file, but I've replaced it with a .jar executable. I still essentially just want to be able to do something simple like:

    make myProgram

How can I modify my Makefile to accomplish this? Thanks for any input/suggestions.

Upvotes: 1

Views: 13457

Answers (2)

Eduardo Dalapicola
Eduardo Dalapicola

Reputation: 83

the code above doesn't working here for me, I make some changes with manifest file:

JAVAC=javac
sources = $(wildcard *.java)
classes = $(sources:.java=.class)

all: myProgram

myProgram: $(classes)

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

jar:    
    @echo "Manifest-Version: 1.0" > manifest.txt
    @echo "Class-Path: ." >> manifest.txt
    @echo "Main-Class: Main" >> manifest.txt
    @echo "" >> manifest.txt

    jar -cmf manifest.txt JARNAME.jar $(classes)

clean:
    rm -f *.class
    rm manifest.txt

Upvotes: 1

paulsm4
paulsm4

Reputation: 121881

Just add a new target (e.g. "jar"), and give it the appropriate "jar" command

EXAMPLE:

JAVAC=javac
sources = $(wildcard *.java)
classes = $(sources:.java=.class)

all: myProgram

myProgram: $(classes)

clean :
    rm -f *.class

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

jar: $(classes)
    jar cvf myjarfile.jar $(classes) 

You can "tweak" this example many ways. For example, you might want to parameterize the "jar" command (e.g. JAR=jar), you might want to create a manifest (perhaps have the makefile itself create a manifest on-the-fly with appropriate "echo" commands), etc. etc.

Here is the Oracle documentation for "jar":

http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Upvotes: 1

Related Questions