在使用命令行工具构建时,如何添加.jar文件依赖项?

mak*_*chi 5 java jar

非常直截了当的问题.可以在不使用蚂蚁或Maven的情况下完成吗?(而且,我的意思是命令行工具)

请注意,我不想创建一个uberjar,我只是希望已归档的单元"知道"它的外部依赖关系.

pto*_*mli 5

假设您正在谈论 的命令行调用javac,那么您正在谈论的是“我可以提供库作为 javac 的参数来满足编译期间的要求”。

顶部条目man javac

   -classpath classpath
          Sets  the user class path, overriding the user class path in the
          CLASSPATH environment variable.  If neither CLASSPATH or -class-
          path  is  specified, the user class path consists of the current
          directory.  See Setting the Class Path for more details.
Run Code Online (Sandbox Code Playgroud)

实际上我怀疑你只需要说

javac -classpath path/to/library1.jar Main.java


ten*_*shi 5

你可以通过META-INF/MANIFEST.MF。您可以像这样将其他 jar 添加到类路径中:

Manifest-Version: 1.0
Main-Class: org.domain.MyMainClass
Class-Path: lib/slf4j-log4j12-1.5.8.jar lib/slf4j-api-1.5.8.jar
Run Code Online (Sandbox Code Playgroud)

我相信,它只有在您Main-Class像这样定义和启动应用程序时才有效:

java -jar my-app.jar
Run Code Online (Sandbox Code Playgroud)

另请注意,类路径路径是相对于主 jar 的。所以在我的示例目录结构应该是这样的:

  • 我的应用程序.jar
    • slf4j-log4j12-1.5.8.jar
    • slf4j-api-1.5.8.jar