Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassNotFoundException when running hello app #1092

Closed
mrvisser opened this issue Mar 30, 2019 · 3 comments
Closed

ClassNotFoundException when running hello app #1092

mrvisser opened this issue Mar 30, 2019 · 3 comments

Comments

@mrvisser
Copy link

Description

Getting ClassNotFoundException when running both hello world app, and my own app that was previously working.

Steps to reproduce

bvisser@bb in ~/Source
$ sam --version
SAM CLI, version 0.14.2
bvisser@bb in ~/Source
$ python --version
Python 2.7.10
bvisser@bb in ~/Source
$ docker --version
Docker version 18.09.2, build 6247962
bvisser@bb in ~/Source
$ sam init --runtime java8
[+] Initializing project structure...

Project generated: ./sam-app

Steps you can take next within the project folder
===================================================
[*] Install dependencies
[*] Invoke Function: sam local invoke HelloWorldFunction --event event.json
[*] Start API Gateway locally: sam local start-api

Read sam-app/README.md for further instructions

[*] Project initialization is now complete
bvisser@bb in ~/Source
$ cd sam-app/HelloWorldFunction/
bvisser@bb in ~/Source/sam-app/HelloWorldFunction
$ mvn compile package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/bvisser/Source/sam-app/HelloWorldFunction/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloWorld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/bvisser/Source/sam-app/HelloWorldFunction/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/bvisser/Source/sam-app/HelloWorldFunction/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloWorld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/bvisser/Source/sam-app/HelloWorldFunction/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloWorld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/bvisser/Source/sam-app/HelloWorldFunction/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloWorld ---
[INFO] Surefire report directory: /Users/bvisser/Source/sam-app/HelloWorldFunction/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.822 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloWorld ---
[INFO] Building jar: /Users/bvisser/Source/sam-app/HelloWorldFunction/target/HelloWorld-1.0.jar
[INFO]
[INFO] --- maven-shade-plugin:3.1.1:shade (default) @ HelloWorld ---
[INFO] Including com.amazonaws:aws-lambda-java-core:jar:1.2.0 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/bvisser/Source/sam-app/HelloWorldFunction/target/HelloWorld-1.0.jar with /Users/bvisser/Source/sam-app/HelloWorldFunction/target/HelloWorld-1.0-shaded.jar
[INFO] Dependency-reduced POM written at: /Users/bvisser/Source/sam-app/HelloWorldFunction/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.923 s
[INFO] Finished at: 2019-03-30T17:40:41Z
[INFO] ------------------------------------------------------------------------
bvisser@bb in ~/Source/sam-app/HelloWorldFunction
$ cd ..
bvisser@bb in ~/Source/sam-app
$ sam local start-api --debug --profile performance --region us-east-1
2019-03-30 17:43:36 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-03-30 17:43:36 Changing event name from before-call.apigateway to before-call.api-gateway
2019-03-30 17:43:36 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-03-30 17:43:36 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-03-30 17:43:36 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-03-30 17:43:36 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-03-30 17:43:36 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-03-30 17:43:36 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-03-30 17:43:36 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-03-30 17:43:36 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-03-30 17:43:36 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-03-30 17:43:36 Setting config variable for profile to 'performance'
2019-03-30 17:43:36 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-03-30 17:43:36 Changing event name from before-call.apigateway to before-call.api-gateway
2019-03-30 17:43:36 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-03-30 17:43:36 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-03-30 17:43:36 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-03-30 17:43:36 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-03-30 17:43:36 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-03-30 17:43:36 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-03-30 17:43:36 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-03-30 17:43:36 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-03-30 17:43:36 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-03-30 17:43:36 Setting config variable for profile to 'performance'
2019-03-30 17:43:36 Setting config variable for region to 'us-east-1'
2019-03-30 17:43:36 Using SAM Template at /Users/bvisser/Source/sam-app/template.yaml
2019-03-30 17:43:36 local start-api command is called
2019-03-30 17:43:36 No Parameters detected in the template
2019-03-30 17:43:36 2 resources found in the template
2019-03-30 17:43:36 Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2019-03-30 17:43:36 Trying paths: ['/Users/bvisser/.docker/config.json', '/Users/bvisser/.dockercfg']
2019-03-30 17:43:36 Found file at path: /Users/bvisser/.docker/config.json
2019-03-30 17:43:36 Found 'auths' section
2019-03-30 17:43:36 Auth data for https://240540759983.dkr.ecr.us-east-1.amazonaws.com is absent. Client might be using a credentials store instead.
2019-03-30 17:43:36 Auth data for https://index.docker.io/v1/ is absent. Client might be using a credentials store instead.
2019-03-30 17:43:36 Auth data for 240540759983.dkr.ecr.us-east-1.amazonaws.com is absent. Client might be using a credentials store instead.
2019-03-30 17:43:36 Found 'credsStore' section
2019-03-30 17:43:36 http://localhost:None "GET /v1.35/_ping HTTP/1.1" 200 2
2019-03-30 17:43:36 No Parameters detected in the template
2019-03-30 17:43:36 2 resources found in the template
2019-03-30 17:43:36 Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2019-03-30 17:43:36 Detected Inline Swagger definition
2019-03-30 17:43:36 Lambda function integration not found in Swagger document at path='/hello' method='get'
2019-03-30 17:43:36 Found '0' APIs in resource 'ServerlessRestApi'
2019-03-30 17:43:36 Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs
2019-03-30 17:43:36 1 APIs found in the template
2019-03-30 17:43:36 Skipping environment variable credential check because profile name was explicitly set.
2019-03-30 17:43:36 Looking for credentials via: assume-role
2019-03-30 17:43:36 Looking for credentials via: shared-credentials-file
2019-03-30 17:43:36 Found credentials in shared credentials file: ~/.aws/credentials
2019-03-30 17:43:36 Loading JSON file: /usr/local/Cellar/aws-sam-cli/0.14.2/libexec/lib/python3.7/site-packages/botocore/data/endpoints.json
2019-03-30 17:43:36 Event choose-service-name: calling handler <function handle_service_name_alias at 0x10f3e5950>
2019-03-30 17:43:36 Loading JSON file: /usr/local/Cellar/aws-sam-cli/0.14.2/libexec/lib/python3.7/site-packages/botocore/data/lambda/2015-03-31/service-2.json
2019-03-30 17:43:36 Event creating-client-class.lambda: calling handler <function add_generate_presigned_url at 0x10f3aa488>
2019-03-30 17:43:36 The s3 config key is not a dictionary type, ignoring its value of: None
2019-03-30 17:43:36 Setting lambda timeout as (60, 60)
2019-03-30 17:43:36 Loading JSON file: /usr/local/Cellar/aws-sam-cli/0.14.2/libexec/lib/python3.7/site-packages/botocore/data/_retry.json
2019-03-30 17:43:36 Registering retry handlers for service: lambda
2019-03-30 17:43:36 Trying paths: ['/Users/bvisser/.docker/config.json', '/Users/bvisser/.dockercfg']
2019-03-30 17:43:36 Found file at path: /Users/bvisser/.docker/config.json
2019-03-30 17:43:36 Found 'auths' section
2019-03-30 17:43:36 Auth data for https://240540759983.dkr.ecr.us-east-1.amazonaws.com is absent. Client might be using a credentials store instead.
2019-03-30 17:43:36 Auth data for https://index.docker.io/v1/ is absent. Client might be using a credentials store instead.
2019-03-30 17:43:36 Auth data for 240540759983.dkr.ecr.us-east-1.amazonaws.com is absent. Client might be using a credentials store instead.
2019-03-30 17:43:36 Found 'credsStore' section
2019-03-30 17:43:36 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2019-03-30 17:43:36 You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-03-30 17:43:36 Localhost server is starting up. Multi-threading = True
2019-03-30 17:43:36  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2019-03-30 17:43:42 Constructed String representation of Event to invoke Lambda. Event: {"httpMethod": "GET", "body": null, "resource": "/hello", "requestContext": {"resourceId": "123456", "apiId": "1234567890", "resourcePath": "/hello", "httpMethod": "GET", "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", "accountId": "123456789012", "stage": "prod", "identity": {"apiKey": null, "userArn": null, "cognitoAuthenticationType": null, "caller": null, "userAgent": "Custom User Agent String", "user": null, "cognitoIdentityPoolId": null, "cognitoAuthenticationProvider": null, "sourceIp": "127.0.0.1", "accountId": null}, "extendedRequestId": null, "path": "/hello"}, "queryStringParameters": null, "headers": {"Host": "127.0.0.1:3000", "User-Agent": "curl/7.54.0", "Accept": "*/*", "X-Forwarded-Proto": "http", "X-Forwarded-Port": "3000"}, "pathParameters": null, "stageVariables": null, "path": "/hello", "isBase64Encoded": false}
2019-03-30 17:43:42 Found one Lambda function with name 'HelloWorldFunction'
2019-03-30 17:43:42 Invoking helloworld.App::handleRequest (java8)
2019-03-30 17:43:42 Environment variables overrides data is standard format
2019-03-30 17:43:42 Loading AWS credentials from session with profile 'performance'
2019-03-30 17:43:42 Resolving code path. Cwd=/Users/bvisser/Source/sam-app, CodeUri=HelloWorldFunction
2019-03-30 17:43:42 Resolved absolute path to code is /Users/bvisser/Source/sam-app/HelloWorldFunction
2019-03-30 17:43:42 Code /Users/bvisser/Source/sam-app/HelloWorldFunction is not a zip/jar file
2019-03-30 17:43:42 Skipping building an image since no layers were defined
2019-03-30 17:43:42 Trying paths: ['/Users/bvisser/.docker/config.json', '/Users/bvisser/.dockercfg']
2019-03-30 17:43:42 Found file at path: /Users/bvisser/.docker/config.json
2019-03-30 17:43:42 Found 'auths' section
2019-03-30 17:43:42 Auth data for https://240540759983.dkr.ecr.us-east-1.amazonaws.com is absent. Client might be using a credentials store instead.
2019-03-30 17:43:42 Auth data for https://index.docker.io/v1/ is absent. Client might be using a credentials store instead.
2019-03-30 17:43:42 Auth data for 240540759983.dkr.ecr.us-east-1.amazonaws.com is absent. Client might be using a credentials store instead.
2019-03-30 17:43:42 Found 'credsStore' section
2019-03-30 17:43:42 http://localhost:None "GET /v1.35/images/lambci/lambda:java8/json HTTP/1.1" 200 None
2019-03-30 17:43:42 Looking for auth config
2019-03-30 17:43:42 Using credentials store "osxkeychain"
2019-03-30 17:43:42 Looking for auth entry for 'https://index.docker.io/v1/'
2019-03-30 17:43:42 Found auth config
2019-03-30 17:43:43 http://localhost:None "POST /v1.35/images/create?tag=java8&fromImage=lambci%2Flambda HTTP/1.1" 200 None

Fetching lambci/lambda:java8 Docker container image......
2019-03-30 17:43:43 Mounting /Users/bvisser/Source/sam-app/HelloWorldFunction as /var/task:ro inside runtime container
2019-03-30 17:43:44 http://localhost:None "POST /v1.35/containers/create HTTP/1.1" 201 90
2019-03-30 17:43:44 http://localhost:None "GET /v1.35/containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d/json HTTP/1.1" 200 None
2019-03-30 17:43:44 http://localhost:None "GET /v1.35/containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d/json HTTP/1.1" 200 None
2019-03-30 17:43:44 http://localhost:None "POST /v1.35/containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d/start HTTP/1.1" 204 0
2019-03-30 17:43:44 Starting a timer for 20 seconds for function 'HelloWorldFunction'
2019-03-30 17:43:44 http://localhost:None "GET /v1.35/containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d/json HTTP/1.1" 200 None
2019-03-30 17:43:44 http://localhost:None "POST /containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d/attach?stdout=1&stderr=1&logs=1&stream=1&stdin=0 HTTP/1.1" 101 0
START RequestId: ee7c76ab-a75f-4afa-8249-72f5552040d2 Version: $LATEST
java.lang.ClassNotFoundException: helloworld.App
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	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)

END RequestId: ee7c76ab-a75f-4afa-8249-72f5552040d2
REPORT RequestId: ee7c76ab-a75f-4afa-8249-72f5552040d2	Duration: 3.02 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 4 MB
2019-03-30 17:43:45 http://localhost:None "GET /v1.35/containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d/json HTTP/1.1" 200 None
2019-03-30 17:43:45 http://localhost:None "DELETE /v1.35/containers/e374b08d13d5cbcea3ef01bf915cafa45b59db9829844796a2bdffd4103f136d?v=False&link=False&force=True HTTP/1.1" 204 0
2019-03-30 17:43:45 Function returned an invalid response (must include one of: body, headers or statusCode in the response object). Response received:
2019-03-30 17:43:45 127.0.0.1 - - [30/Mar/2019 17:43:45] "GET /hello HTTP/1.1" 502 -

Observed result

An exception after sending a GET request to the endpoint:

curl request:

bvisser@bb in ~
$ curl http://127.0.0.1:3000/hello
{"message":"Internal server error"}

Lambda output:

START RequestId: ee7c76ab-a75f-4afa-8249-72f5552040d2 Version: $LATEST
java.lang.ClassNotFoundException: helloworld.App
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	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)

END RequestId: ee7c76ab-a75f-4afa-8249-72f5552040d2
REPORT RequestId: ee7c76ab-a75f-4afa-8249-72f5552040d2	Duration: 3.02 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 4 MB

Expected result

I'd expect the Lambda to be able to find the class on the classpath and run the handler function.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Mac OSX High Sierra (10.13.3 (17D47))
  2. sam --version:
bvisser@bb in ~/Source
$ sam --version
SAM CLI, version 0.14.2
bvisser@bb in ~/Source
$ python --version
Python 2.7.10
bvisser@bb in ~/Source
$ docker --version
Docker version 18.09.2, build 6247962

Docker shared directories (I eventually added /var/folders to try and resolve the issue):

/Users
/Volumes
/private
/tmp
/var/folders
bvisser@bb in ~/Source
$ python -c "import tempfile; print(tempfile.gettempdir())"
/var/folders/76/t461w_fd2ws1kh43fmsm299wwv8hr7/T

I'm not sure what else to try, do you have any suggestions? This used to work, I can't think of what could have changed... maybe a Docker auto-update?

@mrvisser
Copy link
Author

mrvisser commented Mar 31, 2019

a) This is throwing this error because I didn't run sam build in the hello app, which I guess is necessary to generate something under HelloWorldApp for the default configuration
b) The ClassNotFoundException is throwing in my other project because of a slight change in location of the jar file referenced in the CodeUri

The problem here is that maybe sam-cli needs to do a better job at sensing an incorrect configuration and throw a more helpful error? It seems like a ClassNotFoundException is a pretty wide blanket for a number of things that could go wrong (No access to mount under docker, the CodeUri target does not exist at all, the actual class being targeted cannot be found on the classpath).

Are all of the above acceptable scenarios or not possible to validate when running sam local?

@sriram-mv
Copy link
Contributor

The problem here is that maybe sam-cli needs to do a better job at sensing an incorrect configuration and throw a more helpful error? It seems like a ClassNotFoundException is a pretty wide blanket for a number of things that could go wrong (No access to mount under docker, the CodeUri target does not exist at all, the actual class being targeted cannot be found on the classpath).

These seem to be good suggestions around overall validation before we invoke, do you want to create a separate issue that is across runtimes and not just java?

@sriram-mv
Copy link
Contributor

Are you able to repro this still this with latest version of SAM CLI, we can open a separate issue for this in terms of better error messages, But i'm closing this particular issue as the underlying question is answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants