AWS SDK Error: A JNI error has occurred, … java.lang.NoClassDefFoundError

I am trying to do the tutorial here using the SDK specified here.

Specifically, I am trying to execute the following Amazon Web Services DynamoDB sample code with Maven:

https://github.com/JohnReedLOL/DynamoDBTutorial/blob/master/src/main/java/org/example/basicapp/MoviesCreateTable.java

Like so…

c4b301bb8ad9:myapp StackOverflowUser$ pwd /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp c4b301bb8ad9:myapp StackOverflowUser$ ./../mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myapp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myapp --- [INFO] Surefire report directory: /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.example.basicapp.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myapp --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.557 s [INFO] Finished at: 2018-01-20T13:37:59-05:00 [INFO] Final Memory: 12M/225M [INFO] ------------------------------------------------------------------------

But when I try to run it I get this error…

c4b301bb8ad9:myapp StackOverflowUser$ java -cp target/myapp-1.0-SNAPSHOT.jar org.example.basicapp.MoviesCreateTable Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/services/dynamodbv2/document/Table at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.dynamodbv2.document.Table at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more c4b301bb8ad9:myapp StackOverflowUser$

The Java version I am running is…

StackOverflowUser$ java -version java version "1.8.0_151" Java(TM) SE Runtime Environment (build 1.8.0_151-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

And I am running this jar file with this Mac version…

I tried adding to pom.xml:

<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>org.example.basicapp.MoviesCreateTable</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>

And then:

$ ./../mvn clean compile assembly:single

But when I run it I get:

$ java -cp target/myapp-1.0-SNAPSHOT.jar org.example.basicapp.MoviesCreateTable Error: Could not find or load main class org.example.basicapp.MoviesCreateTable

Solution:

The classloader can’t load a class you are importing (com/amazonaws/services/dynamodbv2/document/Table.

You can prove this by running unzip -l myapp-1.0-SNAPSHOT.jar

It is missing because your final jar artifact is packed without its dependency (aws-java-sdk).

Please check if this dependency is reachable from your remote repository (in this case, maven central) and is downloaded to your local .m2 repository.

You can include it explicitly by using maven-assembly-plugin:

<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>org.example.basicapp.MoviesCreateTable</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>

And then package it by running mvn clean compile assembly:single.

A new jar will be created – myapp-1.0-SNAPSHOT-jar-with-dependencies.jar (along side your original jar),and now you don’t have to specifiy the main class (you already did in the plugin configuration and it set it up to you on the jar manifest), just run java -jar target/myapp-1.0-SNAPSHOT-jar-with-dependencies.jar

Advertisements Rate this:Share this:
Like this:Like Loading...