I am a university student working on a research project that involves migrating a web application to Amazon's cloud. I spent the last semester working with servlets and was able to get the majority of the app's functionality working using a java servlet running in Tomcat.
I am now trying to integrate Amazon Web Services such as the SimpleDB and E-mail service. I develop using the Eclipse Java EE for Web Developers Indigo IDE. I have the AWS plugin installed and am able to run sample code that Amazon provides to call SDB and SES successfully.
However when I attempt to call a service like simpleDB from the servlet either directly or through a separate class I receive the following error:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class org.comtor.cloud.api.APIservlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:680) root cause
java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials java.lang.Class.getDeclaredConstructors0(Native Method) java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) java.lang.Class.getConstructor0(Class.java:2699) java.lang.Class.newInstance0(Class.java:326) java.lang.Class.newInstance(Class.java:308) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:680) root cause
java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533) java.lang.Class.getDeclaredConstructors0(Native Method) java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) java.lang.Class.getConstructor0(Class.java:2699) java.lang.Class.newInstance0(Class.java:326) java.lang.Class.newInstance(Class.java:308) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:680) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.
Apache Tomcat/7.0.23
My code looks like this:
package org.comtor.cloud.api;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.CreateDomainRequest;
import java.io.IOException;
@SuppressWarnings("serial")
public class APIservlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
// AWS Credentials
String sAccessKey = "myAccessKey";
String sSecretKey = "mySecretKey";
// Authenticate AWS account
BasicAWSCredentials oAWSCredentials = new BasicAWSCredentials(sAccessKey, sSecretKey);
AmazonSimpleDBClient awsSimpleDBClient = new AmazonSimpleDBClient(oAWSCredentials);
// Try to access simpleDB
try
{
System.out.println("Connecting to Simple Database");
// Create a domain
String myDomain = "MyStore";
System.out.println("Creating domain called " + myDomain + ".\n");
awsSimpleDBClient.createDomain(new CreateDomainRequest(myDomain));
System.out.println("Connection Made");
}
// Print Error Stack Trace
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
// Close PrintWriter
out.close();
}
}
** Access & Secret Keys removed from code **
The same goes for SES. I have also tried using a java library like 'typica' and still receive a NoClassDef error. I have the AWS library in my build path under the libraries folder and the jar package / classes seem to be there. So I am unsure why I am unable to use these classes to interact with simpleDB from my servlet.
I'd greatly appreciate any help on this matter. I believe I am not fully understanding how to call AWS SDB from a web environment.
Thank you.
java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials indicates that this class is not on your classpath. Make sure that the jar containing the com.amazonaws.auth.AWSCredentials class is included in your web application's WEB-INF/lib folder.
来源:https://stackoverflow.com/questions/8936274/creating-domain-with-aws-simpledb-from-java-servlet