java.lang.ClassNotFoundException / NoClassDefFound

2020-02-10 23:14发布

问题:

I get the following error when trying to run a java program that uses jackon's ObjectMapper class:

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

I have the Jackson annotations, databind (which contains ObjectMapper), and core JARs in my Maven repository under ~/.m2/repository/com/fasterxml/jackson/core/jackson-[packagename]/2.4.0. here's the dependencies section of my 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>

here's my CLASSPATH:

:/Users/myusername/Library/Java/Extensions:/Library/Java/Extensions:/Users/myusername/.m2/repository

so Maven knows all about these JARs - where to get em, where to put em, etc. - but java itself can't seem to find the classes I need. what am I doing wrong?? thanks!

回答1:

try to redo maven lifecycle goals:

mvn clean install -U


回答2:

The default maven plugin doesn't build a fat jar with dependencies.

To build a jar bundled with its dependencies so that we can execute it with java -jar, we can use maven-assembly-plugin, which packages the jar with the name xxx-jar-with-dependencies.jar.

Here is a sample 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>

Now you should be able to run your jar with

java -jar xxx-jar-with-dependencies.jar