0

我有一个 java 项目,需要编写 makefile 让它在 Linux 上运行。我的项目包括外部jar文件和资源包(.txt资源)。我真的是Linux新手,只是学习如何编写makefile。

我的项目结构

我参考了一些材料并编写了这样的makefile:

# 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

但我不知道如何将这两个外部 .jar 文件(gson.jar、commons.jar)添加到 makefile 中。而且我不太确定我写的文件路径是否正确。

4

2 回答 2

1

javac有一个-cpand-classpath论点:

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

就文档而言,它们似乎是等效的。

于 2012-10-02T05:03:07.890 回答
1

我通过将所有 *.jar 文件添加到新文件夹“lib”来解决问题。然后 javac -sourcepath src/ -classpath lib/*.jar 将解决外部jar文件问题。

于 2012-10-02T23:19:42.303 回答