0

AWS provide this Java code to perform video analysis in rekognition. However when viewing this in Eclipse there is a error message :

The public type JobCompletionHandler must be defined in its own file

When the function is called in AWS it also complains in the cloudwatch logs with :

Unresolved compilation problem: The public type JobCompletionHandler must be defined in its own file : java.lang.Error java.lang.Error: Unresolved compilation problem: The public type JobCompletionHandler must be defined in its own file at com.amazonaws.lambda.demo.JobCompletionHandler.<init>(LambdaFunctionHandler.java:19) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Met
Unresolved compilation problem:
The public type JobCompletionHandler must be defined in its own file
: java.lang.Error
java.lang.Error: Unresolved compilation problem:
The public type JobCompletionHandler must be defined in its own file

Here is the full function provided by AWS :

//Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazonrekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
import java.util.List;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.GetLabelDetectionRequest;
import com.amazonaws.services.rekognition.model.GetLabelDetectionResult;
import com.amazonaws.services.rekognition.model.LabelDetection;
import com.amazonaws.services.rekognition.model.LabelDetectionSortBy;
import com.amazonaws.services.rekognition.model.VideoMetadata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

Can rename this public class and file name. Did not need to create a separate file.

public class JobCompletionHandler implements RequestHandler<SNSEvent, String> {
    @Override
    public String handleRequest(SNSEvent event, Context context) {
        String message = event.getRecords().get(0).getSNS().getMessage();
        LambdaLogger logger = context.getLogger();
        // Parse SNS event for analysis results. Log results
        try {
            ObjectMapper operationResultMapper = new ObjectMapper();
            JsonNode jsonResultTree = operationResultMapper.readTree(message);
            logger.log("Rekognition Video Operation:=========================");
            logger.log("Job id: " + jsonResultTree.get("JobId"));
            logger.log("Status : " + jsonResultTree.get("Status"));
            logger.log("Job tag : " + jsonResultTree.get("JobTag"));
            logger.log("Operation : " + jsonResultTree.get("API"));
            if (jsonResultTree.get("API").asText().equals("StartLabelDetection")) {
                if (jsonResultTree.get("Status").asText().equals("SUCCEEDED")){
                    GetResultsLabels(jsonResultTree.get("JobId").asText(), context);
                }
                else{
                    String errorMessage = "Video analysis failed for job "
                    + jsonResultTree.get("JobId")
                    + "State " + jsonResultTree.get("Status");
                    throw new Exception(errorMessage);
                }
            } else
            logger.log("Operation not StartLabelDetection");
        } catch (Exception e) {
            logger.log("Error: " + e.getMessage());
            throw new RuntimeException (e);
        }
        return message;
    }
    void GetResultsLabels(String startJobId, Context context) throws Exception {
        LambdaLogger logger = context.getLogger();
        AmazonRekognition rek =
        AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
        int maxResults = 1000;
        String paginationToken = null;
        GetLabelDetectionResult labelDetectionResult = null;
        String labels = "";
        Integer labelsCount = 0;
        String label = "";
        String currentLabel = "";
        //Get label detection results and log them.
        do {
            GetLabelDetectionRequest labelDetectionRequest = new
            GetLabelDetectionRequest().withJobId(startJobId)
            .withSortBy(LabelDetectionSortBy.NAME).withMaxResults(maxResults).withNextToken(paginationToken);
            labelDetectionResult = rek.getLabelDetection(labelDetectionRequest);
            paginationToken = labelDetectionResult.getNextToken();
            VideoMetadata videoMetaData = labelDetectionResult.getVideoMetadata();
            // Add labels to log
            List<LabelDetection> detectedLabels = labelDetectionResult.getLabels();
            for (LabelDetection detectedLabel : detectedLabels) {
                label = detectedLabel.getLabel().getName();
                if (label.equals(currentLabel)) {
                    continue;
                }
                labels = labels + label + " / ";
                currentLabel = label;
                labelsCount++;
            }
        } while (labelDetectionResult != null && labelDetectionResult.getNextToken() != null);
        logger.log("Total number of labels : " + labelsCount);
        logger.log("labels : " + labels);
    }
}

How can I correct this code so no errors are showing for the public class?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Daniel Baker
  • 75
  • 2
  • 9
  • 1
    Rename the file to suggested name? – madhead Dec 12 '18 at 09:50
  • What's the filename? Is it `JobCompletionHandler.java`? It sounds like you named the file wrong – Krease Dec 12 '18 at 20:01
  • Possible duplicate of ["The public type <> must be defined in its own file" error in Eclipse](https://stackoverflow.com/questions/19814713/the-public-type-classname-must-be-defined-in-its-own-file-error-in-eclipse) – Krease Dec 12 '18 at 20:02
  • After renaming the class as suggested by yourself and Eclipse it now compiles. Now I am faced with another error but I think it is unrelated to this question. – Daniel Baker Dec 13 '18 at 03:22
  • The file name is LamdaFunctionHandler.java – Daniel Baker Dec 13 '18 at 03:29

1 Answers1

-1

After renaming the class it now compiles.

Daniel Baker
  • 75
  • 2
  • 9