Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils

关键词:IntelliJ IDEA、Tomcat、commons-io-2.5.jar、java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils

1、错误提示信息

Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils_第1张图片

图1 运行登录时错误信息

//Tomcat Localhost Log信息
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Servlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause
 java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    at servlet.Servlet.doGet(Servlet.java:29)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1374)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

2、java文件内容

 @WebServlet(name = "Servlet",urlPatterns = "/Servlet")
 public class Servlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         Class c = this.getClass();
         InputStream input = c.getResourceAsStream("a.txt");
         String str = IOUtils.toString(input,"utf-8");//问题所在之处
 System.out.println(str);  } }

Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils_第2张图片

图2 项目目录结构

 

 3、问题解决过程:

①一开始通过IDEA以正常方式导入,目录结构如图1所示,仍旧一样,未能解决,此方法行不通

②在IDEA中Tomcat 9.0.0.M16,中加入commons-io-2.5.jar(此jar包不在Tomcat的lib文件夹中),如图三所示,还是不行。

Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils_第3张图片

图三

③干脆把commons-io-2.5.jar拷贝到Tomcat的lib文件夹中,再在IDEA中导入,结果解决了,目录结构如图四所示。

   至于为什么要把commons-io-2.5.jar拷贝到Tomcat的lib文件夹下,还有望大神们解答。

Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils_第4张图片

图4 解决后的目录结构

你可能感兴趣的:(Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils)