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

12,093

Solution 1

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

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

Solution 2

"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..?

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.

Share:
12,093
Xelian
Author by

Xelian

Just begginerr programer Interests: Java, Gradle, Groovy, Bnd, Spring, JPA. For this moment only ask questions, but soon I will start answering yours. :)

Updated on June 04, 2022

Comments

  • Xelian
    Xelian almost 2 years

    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
    

    And the jar has compile dependency to external library

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

    So I want to execute it comandLine I wrote:

    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.jar in the ...jre7\lib\ext folder. Everything is working.