如何在Windows上使用命令行执行具有外部依赖项的jar文件? [英] How to execute jar file with a external dependency using command line on Windows?

查看:85
本文介绍了如何在Windows上使用命令行执行具有外部依赖项的jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有清单的jar文件

I have jar file with manifest in it

    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

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

And the jar has compile dependency to external library

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

所以我要执行我写的comandLine:

So I want to execute it comandLine I wrote:

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

但是

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

如何在课程路径上添加此文件?

How to add this file on the class path?

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

PS. If I use the "bad way" and copy-paste commons-lang3-3.3.2.jar in the ...jre7\lib\ext folder. Everything is working.

推荐答案

如果不使用-jar选项,则需要指定要运行的主类,因为不会查询清单.

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:

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

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

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).

这篇关于如何在Windows上使用命令行执行具有外部依赖项的jar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆