Freya Ren
Freya Ren

Reputation: 2164

How can I make makefile for java with external jar file?

I have a java project, and need to write makefile to let it run on Linux. My project includes external jar files and resource package(.txt resourses).I am really a newbie for Linux, and just learn how to write makefile.

My project structure

I refer to some materials and write a makefile like this:

# Set the file name of your jar package:
JAR_PKG = ADBproject1.jar

# Set your entry point of your java app:
ENTRY_POINT = adb/Bing_WebResults/Run.java

# Need resource directory
RES_DIR = yes

SOURCE_FILES = \
adb/jsonModels/Metadata.java \
adb/jsonModels/Result.java \
adb/jsonModels/Data.java \
adb/jsonModels/DataContainer.java \
adb/models/Weight_ID.java \
adb/models/Pair.java \
adb/models/Document.java \
adb/models/Collections.java \
adb/Bing_WebResults/Bing_Search.java\
adb/Bing_WebResults/Run.java \


JAVAC = javac
JFLAGS = -encoding UTF-8


vpath %.class bin
vpath %.java src

# show help message by default
Default:
    @echo "make new: new project, create src, bin, res dirs."
    @echo "make build: build project."
    @echo "make clean: clear classes generated."
    @echo "make rebuild: rebuild project."
    @echo "make run: run your app."
    @echo "make jar: package your project into a executable jar."

build: $(SOURCE_FILES:.java=.class)

# pattern rule
%.class: %.java
    $(JAVAC) -cp bin -d bin $(JFLAGS) $<

rebuild: clean build

.PHONY: new clean run jar

new:
ifeq ($(RES_DIR),yes)
    mkdir -pv src bin res
else
    mkdir -pv src bin
endif

clean:
    rm -frv bin/*

run:
    java -cp bin $(ENTRY_POINT)

jar:
ifeq ($(RES_DIR),yes)
    jar cvfe $(JAR_PKG) $(ENTRY_POINT)  -C bin . res
else
    jar cvfe $(JAR_PKG) $(ENTRY_POINT) -C bin .
endif

But I don't know how to add those two external .jar files (gson.jar, commons.jar) into makefile. And I'm not quite sure, whether the file paths I wrote are correct.

Upvotes: 0

Views: 5874

Answers (2)

Freya Ren
Freya Ren

Reputation: 2164

I solve the problem by adding all *.jar files to a new folder "lib". Then javac -sourcepath src/ -classpath lib/*.jar will solve the external jar file problem.

Upvotes: 1

Joey
Joey

Reputation: 354714

javac has a -cp and -classpath argument:

-classpath <path>          Specify where to find user class files and
                           annotation processors
-cp <path>                 Specify where to find user class files and
                           annotation processors

They seem to be equivalent as far as the documentation is concerned.

Upvotes: 1

Related Questions