欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

程序员文章站 2022-04-28 21:24:33
...

老环境:

eclipse  Version : Helios Service Release 2

jdk        Version:1.6

Tomcat Version:6.0

新环境:

eclipse  Version:Oxygen Release (4.7.0)

jdk        Version:1.8

Tomcat Version:8.5.23

在新环境启动程序的时候报错如图:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

        第一步就是想着把Tomcat6下lib目录下的devloader相关class文件拷贝过来:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

拷贝到Tomcat8的lib下

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

启动报错:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

然后查看devloader源码发现改动挺大的,包括过期函数等,造成好几个问题:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

然后根据父类进行修改,中间遇到的问题就不再写了:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

修改好后的正常加载资源启动:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

源码如下:

package org.apache.catalina.loader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;


import javax.servlet.ServletContext;
import org.apache.catalina.LifecycleException;
/**
 * 
* <p>Title: DevLoader</p>  
* <p>Description: JDK1.8,TOMCAT8,ECLIPSE(oxygen64)升级后,修改了兼容性</p>  
* @author huyou  
* @date 2018年1月30日
 */
public class DevLoader extends WebappLoader {  
    private static final String info = "org.apache.catalina.loader.DevLoader/1.0";  
    private String webClassPathFile = ".#webclasspath";  
    private String tomcatPluginFile = ".tomcatplugin";  
  
    public DevLoader() {  
    }  
  
    public DevLoader(ClassLoader parent) {  
        super(parent);  
    }  
  
    public void startInternal() throws LifecycleException {  
        log("Starting DevLoader");  
  
        super.startInternal();  
  
        ClassLoader cl = super.getClassLoader();
		if (cl instanceof ParallelWebappClassLoader == false) {
			logError("Unable to install WebappClassLoader !");
			return;
		}        
       
		ParallelWebappClassLoader devCl = (ParallelWebappClassLoader) cl;  
        List webClassPathEntries = readWebClassPathEntries();  
        StringBuffer classpath = new StringBuffer();  
        for (Iterator it = webClassPathEntries.iterator(); it.hasNext();) {  
            String entry = (String) it.next();  
            File f = new File(entry);  
            if (f.exists()) {  
                if ((f.isDirectory()) && (!(entry.endsWith("/"))))  
                    f = new File(entry + "/");  
                try {  
                	
                    URL url = f.toURI().toURL();
  
//                     devCl.addRepository(url.toString());  
                    devCl.addURL(url);  
                    classpath.append(f.toString() + File.pathSeparatorChar);  
                    log("added " + url.toString());  
                } catch (MalformedURLException e) {  
                    logError(entry + " invalid (MalformedURL)");  
                }  
            } else {  
                logError(entry + " does not exist !");  
            }  
        }  
  
        String cp = (String) getServletContext().getAttribute("org.apache.catalina.jsp_classpath");  
        StringTokenizer tokenizer = new StringTokenizer(cp, File.pathSeparatorChar + "");  
        while (tokenizer.hasMoreTokens()) {  
            String token = tokenizer.nextToken();  
  
            if ((token.charAt(0) == '/') && (token.charAt(2) == ':')) {  
                token = token.substring(1);  
            }  
            classpath.append(token + File.pathSeparatorChar);  
        }  
  
        getServletContext().setAttribute("org.apache.catalina.jsp_classpath", classpath.toString());  
        log("JSPCompiler Classpath = " + classpath);  
    }  
  
    protected void log(String msg) {  
        System.out.println("[DevLoader] " + msg);  
    }  
  
    protected void logError(String msg) {  
        System.err.println("[DevLoader] Error: " + msg);  
    }  
  
    protected List readWebClassPathEntries() {  
        List rc = null;  
  
        File prjDir = getProjectRootDir();  
        if (prjDir == null)  
            return new ArrayList();  
  
        log("projectdir=" + prjDir.getAbsolutePath());  
  
        rc = loadWebClassPathFile(prjDir);  
  
        if (rc == null)  
            rc = new ArrayList();  
        return rc;  
    }  
  
    protected File getProjectRootDir() {  
        File rootDir = getWebappDir();  
        FileFilter filter = new FileFilter() {  
            public boolean accept(File file) {  
                return (file.getName().equalsIgnoreCase(webClassPathFile) || file.getName().equalsIgnoreCase(tomcatPluginFile));  
            }  
        };  
        while (rootDir != null) {  
            File[] files = rootDir.listFiles(filter);  
            if ((files != null) && (files.length >= 1))  
                return files[0].getParentFile();  
  
            rootDir = rootDir.getParentFile();  
        }  
        return null;  
    }  
  
    protected List loadWebClassPathFile(File prjDir) {  
        File cpFile = new File(prjDir, this.webClassPathFile);  
        if (cpFile.exists()) {  
            FileReader reader = null;  
            try {  
                List rc = new ArrayList();  
                reader = new FileReader(cpFile);  
                LineNumberReader lr = new LineNumberReader(reader);  
                String line = null;  
                while ((line = lr.readLine()) != null) {  
                    line = line.replace('\\', '/');  
                    rc.add(line);  
                }  
                return rc;  
            } catch (IOException ioEx) {  
                if (reader != null)  
                    ;  
                return null;  
            }  
        }  
        return null;  
    }  
  
    protected ServletContext getServletContext() {  
        // return ((Context) getContainer()).getServletContext();  
        return this.getContext().getServletContext();  
    }  
  
    protected File getWebappDir() {  
        File webAppDir = new File(getServletContext().getRealPath("/"));  
        return webAppDir;  
    }  
}  



解决方案,在普通工程中新建包:org.apache.catalina.loader   新建类DevLoader并继承WebappLoader 如图:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

然后将上面的代码全部复制到DevLoader类中,将此工程清理下,打开此工程文件夹:

升级Java开发工具遇到java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader的解决

将org文件夹整个拷贝纸Tomcat下的lib目录下,启动就成!


想直接下载用的话,请在这下载:

http://download.csdn.net/download/weixin_38374577/10232353