使用Google App Engine for Java的ClassNotFoundException

时间:2011-09-18 22:52:02

标签: google-app-engine

我和朋友正在使用Java SDK进入App Engine。我们已经 把“你好,世界”的例子放在一起没有问题;但是,何时 我们尝试添加代码以使用。来简单地调用web服务 com.google.api.client.http中的类,我们的代码编译得很好但是 导致运行时错误:

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)

(...切)

有问题的代码是:

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;
               }
       }
}

我们最好的猜测是构建类路径与构建类路径之间的差异 运行时类路径,但我们的运行时类路径似乎没问题。该 HttpTransport类位于google-api-client-1.4.1-beta.jar中 库,这包含在Eclipse生成的命令行中 运行项目:

/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.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

之前有没有人遇到过这个问题,或者有没有人遇到过这个问题 关于我们应该如何调试的建议?我确定它很简单,可能是一个配置问题,但作为新手我们不知道除了类路径之外还要看什么。

3 个答案:

答案 0 :(得分:5)

WEB-INF/lib目录中,您有一些子目录google-api-java-client-1.4.1-betagoogle-api-java-client-1.4.1-beta/dependencies,这些子目录中的jars应该直接位于WEB-INF/lib

答案 1 :(得分:3)

你在WEB-INF / lib中有all of the dependencies吗?

答案 2 :(得分:0)

如果您使用Google Cloud Engine和Eclipse:

将jar文件添加到Appengine项目

  • 它必须位于目录/ {Base Directory} / src / main / webapp / WEB-INF / lib
  • 将jar文件复制到此目录
  • 在Eclipse中->项目->构建路径->配置构建路径->库->添加外部JAR:转到/ {Base Directory} / src / main / webapp / WEB-INF / lib并获取文件