这几天碰到一个诡异的问题,在IBM JDK上二位数组中使用表达式时,出现http 500的错误。500是个内部错误,更web container有关,拿了客户的日志文件,发现异常如下,
<2009-7-23 上午05时01分47秒 CDT>
javax.servlet.ServletException: [HTTP:101249][weblogic.servlet.internal.WebAppServletContext@1aec462 - appName: 'test', name: 'test', context-path: '/test', spec-version: '2.5']: Servlet class jsp_servlet.__test for servlet /test.jsp could not be loaded because the requested class was not found in the classpath .
java.lang.ClassFormatError: Illegal class name "[L[Ljava/lang/String;;" in class file jsp_servlet/__test.
......
从具体的异常堆栈来看,应该是class加载时出现了问题,说是类名有问题,但同一context root下类名格式相同的其他page则可以访问。如果去掉二位数组中的表达式,则可以解决这个问题。下面我们通过一个最简单的例子来复现这个问题。
1:IBM JDK version
E:\workspace\eclipse322\src1030\bin>java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build pwi3260sr2-20080818_01(SR2))
IBM J9 VM (build 2.4, J2RE 1.6.0 IBM J9 2.4 Windows XP x86-32 jvmwi3260-20080816
_22093 (JIT enabled, AOT enabled)
J9VM - 20080816_022093_lHdSMr
JIT - r9_20080721_1330ifx2
GC - 20080724_AA)
JCL - 20080808_02
2: __MatrixTest.java
1 package test.classload; 2 3 public class __MatrixTest { 4 public void initialize(){ 5 String sResourcesPath = ""; 6 String sCustomerScale = "123"; 7 String sCustomerType = "123"; 8 String teststr=((!sCustomerScale.equals("")&&sCustomerScale.substring(0,2).equals("02"))?"false":"true"); 9 String sButtons[][] = { 10 {((!sCustomerScale.equals("")&&sCustomerScale.substring(0,2).equals("02"))?"false":"true"), 11 "","Button","新增","新增一条记录","newRecord()",sResourcesPath}, 12 //If above expression is replaced with 'teststr' like below, this issue could be solved. 13 //{teststr,"","Button","新增","新增一条记录","newRecord()",sResourcesPath} 14 }; 15 } 16 } |
3: LoaderTest.java
1 package test.classload; 2 3 public class LoaderTest { 4 5 public static void main(String args[]){ 6 try{ 7 Class clazz = Class.forName("test.classload.__MatrixTest"); 8 __MatrixTest test = (__MatrixTest)clazz.newInstance(); 9 10 System.out.println("class is loaded"); 11 }catch(Exception e){ 12 e.printStackTrace(); 13 } 14 15 } 16 } |
4: 运行结果
这个程序在Sun JDK及BEA JRockit上运行的话,都没有问题。而在IBM JDK1.6上运行的话,会出现如下的如下的异常,
E:\workspace\eclipse322\src1030\bin>java test.classload.LoaderTest
Exception in thread "main" java.lang.ClassFormatError: JVMCFRE068 类名无效;类=test/classload/__MatrixTest,偏移量=0
at java.lang.ClassLoader.defineClassImpl(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:265)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:493)
at java.net.URLClassLoader.access$300(URLClassLoader.java:64)
at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:892)
at java.security.AccessController.doPrivileged(AccessController.java:284
)
at java.net.URLClassLoader.findClass(URLClassLoader.java:414)
at java.lang.ClassLoader.loadClass(ClassLoader.java:643)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:300)
at java.lang.ClassLoader.loadClass(ClassLoader.java:609)
at java.lang.Class.forNameImpl(Native Method)
at java.lang.Class.forName(Class.java:136)
at test.classload.LoaderTest.main(LoaderTest.java:22)
这应该是JDK本身的问题,不知道二维数组中表达式怎么会引发类名解析问题。目前我所知道的解决办法只是把数组中的表达式挪到数组定义外面,就像__MaxtrixTest中所说的一样。
···
···