java 如何在 Windows 上使用命令行执行具有外部依赖项的 jar 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26551718/
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-11-02 10:08:14  来源:igfitidea点击:

How to execute jar file with a external dependency using command line on Windows?

javamavenjarcmd

提问by Xelian

I have jar file with manifest in it

我有一个带有清单的 jar 文件

    Manifest-Version: 1.0
    Build-Jdk: 1.7.0_67
    Created-By: Apache Maven 3.2.3
    Main-Class: com.company.main.Main
    Archiver-Version: Plexus Archiver

And the jar has compile dependency to external library

并且 jar 具有对外部库的编译依赖性

compile 'org.apache.commons:commons-lang3:3.3.2'

So I want to execute it comandLine I wrote:

所以我想执行它 comandLine 我写的:

java -cp C:\commons-lang3-3.3.2.jar -jar myJar-1.0.0.jar

But

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
        at ...

How to add this file on the class path?

如何在类路径上添加这个文件?

PS. If I use the "bad way" and copy-paste commons-lang3-3.3.2.jarin the ...jre7\lib\extfolder. Everything is working.

附注。如果我使用“坏方法”并在...jre7\lib\ext文件夹中复制粘贴commons-lang3-3.3.2.jar。一切正常。

回答by Tom Bunting

If you're not using the -jar option then you need to specify the main class to run, as the manifest will not be interrogated:

如果您没有使用 -jar 选项,那么您需要指定要运行的主类,因为清单不会被询问:

java -cp C:\commons-lang3-3.3.2.jar;.\myJar-1.0.0.jar com.company.main.Main

The classpath (-cp) option is ignored if using the -jar option (in that case the manifest should reference any other required jars via its classpath directive).

如果使用 -jar 选项,则类路径 (-cp) 选项将被忽略(在这种情况下,清单应通过其类路径指令引用任何其他所需的 jar)。

回答by Thirumalai Parthasarathi

"Bad Way"..? You should never include your jar files inside the java directories.. How do you expect the users of your application to use your jar when they are using the standard java..?

“坏道”..?您永远不应该在 java 目录中包含您的 jar 文件。您希望您的应用程序用户在使用标准 java.. 时如何使用您的 jar?

either you can use the command suggested by @tombola82 or you can include the commons-lang jar in your project itself so that you can refer it.

您可以使用@tombola82 建议的命令,也可以在项目本身中包含 commons-lang jar,以便您可以引用它。