今天,在一个项目中,使用了我提供的一个jar,在运行到当JPython试图缓存该jar包中的类时,报解压出错:
java.lang.IllegalArgumentException
at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:284)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:237)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:73)
at org.python.core.CachedJarsPackageManager.getZipPackages(Unknown Source)
at org.python.core.CachedJarsPackageManager.addJarToPackages(Unknown Source)
at org.python.core.CachedJarsPackageManager.addJarToPackages(Unknown Source)
at org.python.core.PathPackageManager.addClassPath(Unknown Source)
at org.python.core.SysPackageManager.findAllPackages(Unknown Source)
at org.python.core.SysPackageManager.<init>(Unknown Source)
at org.python.core.PySystemState.initPackages(Unknown Source)
at org.python.core.PySystemState.initialize(Unknown Source)
at org.python.core.PySystemState.initialize(Unknown Source)
at org.python.core.PySystemState.initialize(Unknown Source)
at org.python.core.PySystemState.initialize(Unknown Source)
at org.python.core.PySystemState.<init>(Unknown Source)
at org.python.util.PythonInterpreter.<init>(Unknown Source)
at org.python.util.InteractiveInterpreter.<init>(Unknown Source)
at org.python.util.InteractiveInterpreter.<init>(Unknown Source)
at org.python.util.InteractiveInterpreter.<init>(Unknown Source)
很奇怪,我的jar应该是没问题的啊。
于是写了个简单的类来测试(核心就是从JPython中复制出来一个函数):
// Extract all of the packages in a single jarfile
public Hashtable getZipPackages(InputStream jarin) throws IOException {
Hashtable zipPackages = new Hashtable();
ZipInputStream zip = new ZipInputStream(jarin);
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
addZipEntry(zipPackages, entry, zip);
zip.closeEntry();
}
// Turn each vector into a comma-separated String
for (Enumeration e = zipPackages.keys(); e.hasMoreElements();) {
Object key = e.nextElement();
Vector[] vec = (Vector[]) zipPackages.get(key);
String classes = vectorToString(vec[0]);
if (vec[1].size() > 0) {
classes += '@' + vectorToString(vec[1]);
}
zipPackages.put(key, classes);
}
return zipPackages;
}
然后运行该类来解析我的jar包,每当执行到:
while ((entry = zip.getNextEntry()) != null)
这一句时就会抛出上面的异常。
用Systemout.print把entry的文件名打印出来,显示执行到其中一个xml文件后就抛异常,我怀疑是那个xml文件前后相关文件有问题,我就把该xml同级包路径里的文件全部删除,异常还是照样出现,但打印出来的最后一个文件名变成其他的了。因为该jar做过混淆,该不会是混淆导致的问题吧,可是暂时也没办法弄个未混淆的jar来测试,所以还是先从其他方面来分析。
看到异常信息中的 getUTF8String 函数,应该是与字符编码方式有关,会不会是中文字符导致的呢,我看了一个jar包中有个说明文件的文件名中有中文字符,我把中文字符删除,再测试,异常不再出现!!
原来是我手工加到jar包中的带中文文件名导致出错。以后得注意这个问题。