开发者

ClassNotFoundException using Google App Engine for Java

A friend and I are dabbling in App Engine using the Java SDK. We've put together a "Hello, World" example with no problems; however, when we try to add code to make a simple call to a webservice using the classes in com.google.api.client.http, our code compiles fine but causes a runtime error:

HTTP ERROR 500

Problem accessing /surreality. Reason:

   com/google/api/client/http/HttpTransport

Caused by:

HTTP ERROR 500

Problem accessing /surreality. Reason:

   com/google/api/client/http/javanet/NetHttpTransport

Caused by:

java.lang.NoClassDefFoundError: com/google/api/client/http/javanet/
NetHttpTransport
       at
com.surreality.scratch.SurrealityServlet.performSearch(SurrealityServlet.java:
31)
       at
com.surreality.scratch.SurrealityServlet.doGet(SurrealityServlet.java:
18)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:
511)

(...cut)

The code in question is:

package com.surreality.scratch;

import java.io.IOException;
import javax.servlet.http.*;
import java.util.List;
import com.google.api.client.googleapis.*;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.*;
import com.google.api.client.util.Key;

@SuppressWarnings("serial")
public class SurrealityServlet extends HttpServlet {

       public void doGet(HttpServletRequest req, HttpServletResponse resp)
                       throws IOException {
               resp.setContentType("text/plain");
               try {
                       this.performSearch(resp);
                       resp.getWriter().println("Here...");
               } catch (Exception e) {
                       resp.getWriter().println("Onoes!");
               }
       }

       public void performSearch(HttpServletResponse resp) throws Exception
{

               try {
                       resp.getWriter().println("Perform Search ....");
                       resp.getWriter().println("-------------------");

                       HttpTransport transport = new NetHttpTransport(); // This line
causes our servlet to implode on every request
               } catch (Exception e) {
                       resp.getWriter().println("failed");
                       throw e;
               }
       }
}

Our best guess was a discrepancy between the build classpath and the runtime classpath, but our runtime classpath seems to be OK. The HttpTransport class is in the google-api-client-1.4.1-beta.jar library, and that's included in the command line Eclipse is generating to run the project:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/
java -Ddatastore.default_high_rep_job_policy_unapplied_job_pct=50 -
Xmx512m -javaagent:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/agent/appengine-agent.jar -
XstartOnFirstThread -Xbootclasspath/p:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/override/appengine-dev-jdk-overrides.jar -
Dfile.encoding=MacRoman -classpath /Users/Arkaaito/Documents/AppEngine/
scratch/surreality/war/WEB-INF/classes:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/appengine-local-runtime-
shared.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/el-api.jar:/Applications/eclipse/
plugins/com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/jsp/repackaged-appengine-
ant-1.7.1.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/jsp/repackaged-appengine-ant-
launcher-1.7.1.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/jsp/repackaged-appengine-
jasper-6.0.29.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/jsp/repackaged-appengine-jasper-
el-6.0.29.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/jsp/repackaged-appengine-tomcat-
juli-6.0.29.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/jsp-api.jar:/Applications/eclipse/
plugins/com开发者_如何学Go.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/shared/servlet-api.jar:/Applications/
eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/appengine-api-1.0-sdk-1.5.3.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/appengine-api-labs-1.5.3.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/appengine-jsr107cache-1.5.3.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/jsr107cache-1.1.jar:/Applications/
eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/orm/datanucleus-
appengine-1.0.9.final.jar:/Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/orm/datanucleus-core-1.1.5.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/orm/datanucleus-jpa-1.1.5.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/orm/geronimo-jpa_3.0_spec-1.1.1.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/orm/geronimo-jta_1.1_spec-1.1.1.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/user/orm/jdo2-api-2.3-eb.jar:/
Applications/eclipse/plugins/
com.google.appengine.eclipse.sdkbundle_1.5.3.r37v201108301710/
appengine-java-sdk-1.5.3/lib/appengine-tools-api.jar:/Users/Arkaaito/
Documents/AppEngine/scratch/surreality/war/WEB-INF/lib/google-api-java-
client-1.4.1-beta/dependencies/httpclient-4.0.3.jar:/Users/Arkaaito/
Documents/AppEngine/scratch/surreality/war/WEB-INF/lib/google-api-java-
client-1.4.1-beta/dependencies/httpcore-4.0.1.jar:/Users/Arkaaito/
Documents/AppEngine/scratch/surreality/war/WEB-INF/lib/google-api-java-
client-1.4.1-beta/google-api-client-1.4.1-beta.jar:/Users/Arkaaito/
Documents/AppEngine/scratch/surreality/war/WEB-INF/lib/google-api-java-
client-1.4.1-beta/google-api-client-googleapis-1.4.1-beta.jar:/Users/
Arkaaito/Documents/AppEngine/scratch/surreality/war/WEB-INF/lib/google-
api-java-client-1.4.1-beta/google-api-client-extensions-1.4.1-
beta.jar:/Users/Arkaaito/Documents/AppEngine/scratch/surreality/war/
WEB-INF/lib/google-api-java-client-1.4.1-beta/google-api-client-
googleapis-extensions-1.4.1-beta.jar:/Users/Arkaaito/Documents/
AppEngine/scratch/surreality/war/WEB-INF/lib/geronimo-
jpa_3.0_spec-1.1.1.jar:/Users/Arkaaito/Documents/AppEngine/scratch/
surreality/war/WEB-INF/lib/geronimo-jta_1.1_spec-1.1.1.jar
com.google.appengine.tools.development.DevAppServerMain --port=3333 /
Users/Arkaaito/Documents/AppEngine/scratch/surreality/war

Has anyone run into this problem before, or does anyone have suggestions on what we should do to debug? I'm sure it's something simple, probably a configuration problem, but as newbies we're not sure what to look at besides the classpaths.


Within your WEB-INF/lib directory, you have some subdirectories google-api-java-client-1.4.1-beta and google-api-java-client-1.4.1-beta/dependencies with jars in those subdirectories that should be directly within WEB-INF/lib


Do you have all of the dependencies in WEB-INF/lib ?


If you work with Google Cloud Engine and Eclipse:

Add jar files to a Appengine Project

  • It must be at Directory /{Base Directory}/src/main/webapp/WEB-INF/lib
  • Copy jar files to this directory
  • In Eclipse -> Project -> Build Path -> Configure Build Path -> Libraries -> Add External JARs: go to the /{Base Directory}/src/main/webapp/WEB-INF/lib and get the files
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜