maven - Command line mvn missing dependency
问题描述
I have a simple program. From the main, I call a URL and it opens. When I run it in intellij, it works fine, but the command line doesn't work. Also, when I create another java program and import this jar, I get the same error.
This is my 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>org.example</groupId>
<artifactId>XMLConverter</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>
</project>
The dependency is missing
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/XML
at xmlconverter.GetXmlData.ReadString(GetXmlData.java:50)
at xmlconverter.GetXmlData.getJson(GetXmlData.java:24)
at xmlconverter.XMLConverter.main(XMLConverter.java:13)
Caused by: java.lang.ClassNotFoundException: org.json.XML
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 3 more
When I run this : mvn dependency:tree I get the right dependency.
My assumption is because I have two .java files. I have a main and then the code. It worked fine when it was all one file XMLConverter.
解决方案
https://www.dropwizard.io/en/latest/getting-started.html#building-fat-jars
Add the following plugin also along with you maven-compiler plugin, to create the jars, mention your main class path under the mainclass tag below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example. <Path to main class></mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins></build>
推荐阅读
- android-studio - 拖放功能在 Android Studio 中不起作用。甚至不能点击显示属性
- c - 尽管条件满足,但循环不会停止
- python - 为什么java重定向失败?
- sql - SQL Server : Procedure, setting value for the variables
- kotlin - Kotlin 1.3.11 has broken null-safety?
- reactjs - 检测用户何时离开单页应用
- mongodb - 使用 akka 流检查 mongo 集合
- class - 如何创建将字段映射到另一个字段的类型(或类)?
- php - Uncaught ArgumentCountError: Too few arguments to function
- javascript - Can't select several rows from mongo database