0

I am trying to build small jar using common cli but i am getting this error after building jar and when i run this command.

java -jar validator.jar 

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/Options
at com.kir.App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.Options
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

My code:

package com.kir;

import org.apache.commons.cli.Options;

public class App {

 public static void main(String[] args) {

   Options options = new Options();

 }

}

pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kircom</groupId>
<artifactId>validator</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.4</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.kir.App</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

Note: I am using IntelliJ. Initially, I thought that jar doesn't include dependency jar while packing. But when I tested with JDBC connection, it worked fine.

VK321
  • 5,768
  • 3
  • 44
  • 47
  • @greg-449 - tag removed. – VK321 Jul 29 '18 at 09:16
  • Can't reproduce. How do you build the jar in the first place? If you built it with Maven, it would be named validator-1.0-SNAPSHOT.jar. So my guess is that you're not using Maven to build it, and the jar thus doesn't contain the commons-cli classes. – JB Nizet Jul 29 '18 at 09:19
  • @JBNizet - I used this steps https://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly – VK321 Jul 29 '18 at 09:20
  • 2
    You shouldn't have. The whole point of using a build tool like Maven and to configure it is to use it to build your project. If you use IntelliJ instead, it defeats the whole purpose. Use Maven to build your jar: `mvn package`. Then run the jar generated by Maven: `java -jar target/validator-1.0-SNAPSHOT.jar`. – JB Nizet Jul 29 '18 at 09:23
  • @JBNizet - thanks with mvn package. it did work! – VK321 Jul 29 '18 at 10:08

0 Answers0