我正在尝试使用通用cli来构建小型jar,但是在构建jar和运行此命令时出现此错误。
java -jar validator.jar
错误:
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
我的代码:
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>
注意:我正在使用IntelliJ。最初,我认为jar在打包时不包括依赖项jar。但是,当我使用JDBC连接进行测试时,它运行良好。