Tomcat 启动失败 IllegalArgumentException: Malformed

本文提供了一种方法来定位并解决在Tomcat启动过程中出现的Malformed错误,通过修改源代码捕获更多错误信息。常见原因是jar包中包含中文文件名。文中还提供了调试Tomcat启动的链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一次启动tomcat的过程中报错:

java.lang.IllegalArgumentException: Malformed 


这个与前面在其他网站上看到的是不同的:java.lang.IllegalArgumentException: Malformed  一般会带有encoding XXX 或者 \u xxxx

但是我看到的错误就只有这个

如何定位:

【1】修改tomcat catanila.jar 中的ContextConfig的源代码:

    protected void processAnnotationsJar(URL url, WebXml fragment,
            boolean handlesTypesOnly) {

        Jar jar = null;
        InputStream is;
        
        try {
//        	String t = url.getPath().substring(6);
//        	t = t.substring(0,t.length()-2);
//        	ZipFile zf = new ZipFile(t, "utf-8");
//    		Enumeration e = zf.getEntries();
//    		while(e.hasMoreElements())
//    		{
//    			System.out.println(e.nextElement());
//    		}
//    		zf.closeQuietly(zf);
            jar = JarFactory.newInstance(url);
            log.info("current jar file is:" + jar.getEntryName() + " jar:" + url.getPath());
            jar.nextEntry();
            String entryName = jar.getEntryName();
            while (entryName != null) {
                if (entryName.endsWith(".class")) {
                    is = null;
                    try {
                        is = jar.getEntryInputStream();
                        processAnnotationsStream(
                                is, fragment, handlesTypesOnly);
                    } catch (IOException e) {
                        log.error(sm.getString("contextConfig.inputStreamJar",
                                entryName, url),e);
                    } catch (ClassFormatException e) {
                        log.error(sm.getString("contextConfig.inputStreamJar",
                                entryName, url),e);
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException ioe) {
                                // Ignore
                            }
                        }
                    }
                }
                jar.nextEntry();
                entryName = jar.getEntryName();
            }
        } catch (IOException e) {
            log.error(sm.getString("contextConfig.jarFile", url), e);
        } finally {
            if (jar != null) {
                jar.close();
            }
        }
    }
这里可以看到只捕获了IOException,为了定位该问题 在catch 部分,修改如下:

    protected void processAnnotationsJar(URL url, WebXml fragment,
            boolean handlesTypesOnly) {

        Jar jar = null;
        InputStream is;
        String entryName = null;
        try {

            jar = JarFactory.newInstance(url);
            log.info("current jar file is:" + jar.getEntryName() + " jar:" + url.getPath());
            jar.nextEntry();
            entryName = jar.getEntryName();
            while (entryName != null) {
                if (entryName.endsWith(".class")) {
                    is = null;
                    try {
                        is = jar.getEntryInputStream();
                        processAnnotationsStream(
                                is, fragment, handlesTypesOnly);
                    } catch (IOException e) {
                        log.error(sm.getString("contextConfig.inputStreamJar",
                                entryName, url),e);
                    } catch (ClassFormatException e) {
                        log.error(sm.getString("contextConfig.inputStreamJar",
                                entryName, url),e);
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException ioe) {
                                // Ignore
                            }
                        }
                    }
                }
                jar.nextEntry();
                entryName = jar.getEntryName();
            }
        } catch (Exception e) {
        	log.warn("current jar:" + url.getPath() + " class:" + entryName);
            log.error(sm.getString("contextConfig.jarFile", url), e);
        } finally {
            if (jar != null) {
                jar.close();
            }
        }
    }


这样就可以在错误的时候获取到详细的信息。(替换catalina.jar 中的ContextConfig*.class ,有内部类)


最后问题得原因一般是:jar包打入了包含中文的文件名 

如果可以直接调试的话还可以参考tomcat的启动调试:http://blog.youkuaiyun.com/scugxl/article/details/37083497

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值