我正在使用Guava 14.0 jar,以下是我的代码
package org.user.module.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.Cache;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class test
*/
@WebServlet("/test")
public class test extends HttpServlet {
private static final long serialVersionUID = 1L;
Cache<String, Integer> map;
public test() {
super();
// TODO Auto-generated constructor stub
}
public void init() throws ServletException
{
map=CacheBuilder.newBuilder().
maximumSize(100).
expireAfterAccess(10, TimeUnit.MINUTES).
build();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String p=request.getParameter("v");
Integer a=map.getIfPresent(p);
if(a==null){
map.put(p, 1);
out.println("Putting The Integer:"+a);
}
else{
map.put(p,a++);
out.println("A is already present and the value is:"+a);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
错误:
SEVERE:为servlet org.user.module.test.test分配异常 java.lang.ClassNotFoundException:com.google.common.cache.Cache at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546) at java.lang.Class.getDeclaredFields0(Native Method)at java.lang.Class.privateGetDeclaredFields(Class.java:2317)at at java.lang.Class.getDeclaredFields(Class.java:1762)at org.apache.catalina.core.DefaultInstanceManager.populateAnnotationsCache(DefaultInstanceManager.java:276) 在 org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:143) 在 org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:125) 在 org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1136) 在 org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:857) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 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:579) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:307) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:722)
从这里下载jar Downlaod Link 链接到我的项目文件夹(项目名称:test2)LINK
答案 0 :(得分:3)
明显的答案是; 从应用程序服务器指定的类路径中找不到Google番石榴罐。 jar应该出现在 WEB-INF / classes 或 WEB-INF / lib 中。 Apache Tomcat还从 $ CATALINA_HOME / lib 文件夹加载公共类。