1

I am trying to incorporate AWS Lambda using Java 8, but when I upload my function to lambda, the class fails to load and I get the following stack trace:

Error loading class com.treyherman.lambda.GifLambdaHandler: com/amazonaws/auth/AWSCredentialsProvider: class java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentialsProvider
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentialsProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more

Here is my Maven depencies in pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.myname.lambda</groupId>
      <artifactId>demo</artifactId>
      <version>4.0.0</version>
      <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-events</artifactId>
      <version>1.3.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk</artifactId>
      <version>1.11.110</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.3.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>
  </dependencies>
</project>

I have read something about the separate jar files in AWS not compiling correctly when I make my file into a jar, but I cannot figure out how to do that.

If anybody knows the solution or has an idea of how to solve it, it'd be much appreciated

hermt2
  • 844
  • 3
  • 14
  • 33
  • 1
    Did you try the maven shade plugin? See [this answer](http://stackoverflow.com/questions/32782980/aws-lambda-noclassdeffounderror#answer-32783493) for details. – Alejandro C De Baca Mar 28 '17 at 20:33
  • Yes this did not work. My function incorporates s3, I commented out all the code and the function ran, but when I uncomment the following line: `AmazonS3 awsS3client = new AmazonS3Client();`, I get this error: – hermt2 Mar 28 '17 at 22:09
  • This Error: `java.lang.NoClassDefFoundError: com/amazonaws/services/s3/AmazonS3Client at com.lambda.demo.LambdaFunctionHandler.` – hermt2 Mar 28 '17 at 22:10
  • @hermt2 can you remove the `compile` from all the `aws` dependencies, then build and run again ? – Amit Mar 28 '17 at 23:04
  • @hermt2 The maven shade plugin is what AWS recommends to [package your classes using maven](http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html). Without doing that, your project will be missing all of its dependencies, exactly the issue you are having. I strongly recommend figuring out what you need to do to make that work. Perhaps you can update the question to include what happens when you run `mvn package`, for example. – Alejandro C De Baca Mar 29 '17 at 02:04
  • I added the plugin, then removed all compile tags but I still get the error. Any other Ideas? – hermt2 Mar 29 '17 at 16:17
  • Did you run `mvn package`? Also, are you running using Eclipse? – Alejandro C De Baca Mar 30 '17 at 01:33
  • Yes, I'll take you through each step I go through. 1. Create new AWS Lambda Java Project 2. Errors are present, and I must alter the Java Compiler within the Properties section to use version 1.6 rather than 1.5, then errors disappear. 3. Remove all `compile` from the pom.xml file 4. Add maven-shade-plugin, then save the project. 5. Go to the command line, type mvn package -- `BUILD SUCCESS` 6. Export lambda project to a .jar file and upload Functions work fine if I do not incorporate any AWS features such as S3, but if I do, then I get the error `NoClassDefFoundError` – hermt2 Mar 30 '17 at 16:38
  • Actually now I see that eclipse generates its own .jar file under the `/target` folder in the projectdir. Only issue now is that it is the .jar file is 10MB over the 50.0MB limit. – hermt2 Mar 30 '17 at 16:55
  • At least I assume that is the only issue. Have not been able to test this new jar file yet. – hermt2 Mar 30 '17 at 16:55
  • Altered the pom.xml file to only include AWS features that I need and it works now. Thank you a ton you're a life saver – hermt2 Mar 30 '17 at 18:55

0 Answers0