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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With