1

java newbie.

I've compiled with java 8

javac HelloWorld.java --release 8

and uploaded the complied file as the aws lambda code.

Why am I getting the error when i run it in aws lambda?

public class HelloWorld
{
    public static void main(String[] args)
    {   
        System.out.println("Hello, World");
    }   
}

Error

An error occurred during JSON parsing: java.lang.RuntimeException
java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: lambdainternal.util.NativeMemoryAsInputStream@ae45eb6; line: 1, column: 1]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: lambdainternal.util.NativeMemoryAsInputStream@ae45eb6; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • Note: you should follow the Java Naming Conventions: class names are written in PascalCase. `helloWorld` should be `HelloWorld`. – MC Emperor Oct 31 '19 at 22:17
  • @MCEmperor Thank You. I did that, but same error. Updated. – Michael Durrant Oct 31 '19 at 22:34
  • 2
    In non-aws world your program looks perfect. :) See if you find it related https://stackoverflow.com/questions/35545642/error-executing-hello-world-for-aws-lambda-in-java – www.hybriscx.com Oct 31 '19 at 22:41
  • May be you need to implement RequestHandler and override handleRequest function https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html – www.hybriscx.com Oct 31 '19 at 22:45

2 Answers2

1

AWS Lambda provides limited Java runtime to execute AWS Java Lambda handlers. The limitations are logical like access to file system, etc for security purposes as the code runs in a shared multi-tenanted environment. In order to execute a program, your class should implement the RequestHandler interface and implement the handleRequest method. Here is the sample from the official documentation

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Hello implements RequestHandler<Integer, String>{
  public String handleRequest(Integer myCount, Context context) {
    return String.valueOf(myCount);
  }
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

You're missing something fundamental here. You can't just throw a class or jar file at Lambda. There's a well-defined interface between the Lambda service and your code. Think of it this way: Lambda launches a JVM, and Lambda itself implements a Java class with the static main method (in its code, not your code) and executes that. That main method will ultimately call your configured Lambda function's entry point. I'd recommend this Hello World tutorial.

jarmod
  • 71,565
  • 16
  • 115
  • 122