Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda JAVA: usage of external libraries

After I upload and run my java .jar file, I get a warning and an error. The warning is:

This function contains external libraries. Uploading a new file will override these libraries.

And The error is:

java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)   

I can build the jar no problem, all dependencies are in my pom. I have a handful of libraries that I'm using, like org.json that also throw the same java.lang.NoClassDefFoundError error. I do import these classes. My jar however doesn't include these files, only my class. I'm suspecting that's related. Is that true? Do I need to find amazon class substitution for these "external" classes that I'm using? I'm confused.

like image 740
user2917629 Avatar asked Jan 02 '17 01:01

user2917629


2 Answers

Amazon Lambda will not download your dependencies for you. It expects your deployment file to contain all dependencies necessary to run your Lambda function. You will need to switch to using the zip deployment method that allows you to include multiple jar files (your Lambda function jar and all dependency jars). Follow the instructions here.

like image 177
Mark B Avatar answered Sep 27 '22 23:09

Mark B


Another option is to create a fat jar file. That is a jar which already contains all your dependency. Such file will be larger to deploy but easier to manage. I would recommend you to use some build tool like Maven to build a fat jar.

like image 35
Mangat Rai Modi Avatar answered Sep 27 '22 23:09

Mangat Rai Modi