尝试运行使用jackon的ObjectMapper类的java程序时出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at com.inin.dynamotransfer.DynamoTransfer.importData(DynamoTransfer.java:133)
at com.inin.dynamotransfer.DynamoTransfer.main(DynamoTransfer.java:67)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
我有Jackson注释,databind(包含ObjectMapper),以及我的Maven存储库中的〜/ .m2 / repository / com / fasterxml / jackson / core / jackson- [packagename] /2.4.0下的核心JAR。这是我的pom.xml的依赖项部分:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
这是我的CLASSPATH:
:/Users/myusername/Library/Java/Extensions:/Library/Java/Extensions:/Users/myusername/.m2/repository
所以Maven知道所有这些JAR - 在哪里获取em,在哪里放em等等 - 但java本身似乎无法找到我需要的类。我究竟做错了什么??谢谢!
答案 0 :(得分:2)
尝试重做maven生命周期目标:
mvn clean install -U
答案 1 :(得分:2)
默认的maven插件不构建带有依赖项的胖jar。
要构建一个与其依赖项捆绑在一起的jar,以便我们可以使用java -jar
执行它,我们可以使用maven-assembly-plugin,它将jar打包为名称xxx-jar-with-dependencies.jar
。
以下是pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.yourMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
现在你应该能够用
运行你的jar了java -jar xxx-jar-with-dependencies.jar