How do I run the psvm in my jar file from AWS Lambda?

Viewed 95

I have a jar file that runs locally if I run java -jar jarname.jar. It has one class called Main in no package. How can I run this with AWS Lambda? I have a Lambda set up but it asks for a Handler (see image) and I don't know what I put in there if I just want to run my jar. I get the errors below when the lambda runs with the configuration below.

2021-03-01T09:01:14.126-05:00   START RequestId: 8f7eea07-3931-4c29-b965-7963fce3b64e Version: $LATEST

2021-03-01T09:01:14.128-05:00   Class not found: Main: java.lang.ClassNotFoundException java.lang.ClassNotFoundException: Main at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348)

2021-03-01T09:01:14.154-05:00   END RequestId: 8f7eea07-3931-4c29-b965-7963fce3b64e

2021-03-01T09:01:14.154-05:00


REPORT RequestId: 8f7eea07-3931-4c29-b965-7963fce3b64e  Duration: 27.60 ms  Billed Duration: 28 ms  Memory Size: 128 MB Max Memory Used: 63 MB  Init Duration: 36.85 ms 
REPORT RequestId: 8f7eea07-3931-4c29-b965-7963fce3b64e Duration: 27.60 ms Billed Duration: 28 ms Memory Size: 128 MB Max Memory Used: 63 MB Init Duration: 36.85 ms

Also, when I tried to upload my jar directly it rejected it so I put it in a zip and it worked.

image

2 Answers

You'll need to add a class to your Java application that implements RequestHandler as documented here. That will be then handler class you specify in the runtime settings.

This is the Documentation for creating a simple Java Lambda.

Handler name will be ClassWithPackage::methodName

AWS will simply create an object for the given class and call the given method(should be public) by passing necessary input. we can't use psvm, since it's static. we need a non-static method.

In your case, since there is no package we can give Main::someMethod in handler, but method name is must, otherwise, it doesn't know which method to call. We could have multiple methods, each representing different lambda function.

if we have dependencies, it is important create a deployment-package, which embeds all the dependent jars and create a single jar file.

Here is the documentation for creating a single-jar. For maven using maven-shade-plugin.

Related