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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6768188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:11:18  来源:igfitidea点击:

How can I add a .jar file dependencies when building it with the command line tool?

javajar

提问by makoshichi

Pretty straightforward question. Can it be done without the use of Ants or Maven? (And by that, I mean the command line tool specifically)

很直接的问题。可以不使用 Ants 或 Maven 来完成吗?(我指的是命令行工具)

Please notice that I don't want to create an uberjar, I just want the archived unit to "know" where its external dependencies are.

请注意,我不想创建 uberjar,我只想让存档单元“知道”其外部依赖项在哪里。

采纳答案by ptomli

Presuming you're talking about a command line invocation of javac, what you're talking about is "can I provide libraries as arguments to javac to fulfill requirements during compilation".

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

Top entry for man javacsays

顶部条目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.
   -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.

Effectively I suspect you just need to say

实际上我怀疑你只需要说

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

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

回答by tenshi

You can make it through META-INF/MANIFEST.MF. You can add other jars to the classpath like this:

你可以通过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

I believe, that it works only if you define Main-Classand start your application like this:

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

java -jar my-app.jar

Also notice, that classpath paths are relative to the main jar. So in my example directory structure should look like this:

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

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

回答by Prasanna Talakanti

I think what you are looking for is a manifest file, Look here for more details http://download.oracle.com/javase/tutorial/deployment/jar/downman.html

我认为您正在寻找的是一个清单文件,请在此处查看更多详细信息 http://download.oracle.com/javase/tutorial/deployment/jar/downman.html