6.9 Checking Verification

To see if a class will pass verification, you need an implementation of the verification algorithm. In early versions of the Java Development Kit, there was a -verify flag to javap that would print whether or not the class passes verification. Adding the -verbose flag would cause javap to print additional information on why verification failed.

Later versions of the JDK omit this option. Instead, you should use java -verify classname. If your class does not have a main, and the class passes verification properly, then java prints a message that main could not be found, which means that it accepted the class but could not run it. If the class does have a main, it will be run.

If the class fails verification, an error is printed. You may be able to get more information using the -verbose flag.

A word of caution: Commercial JVM implementations usually expect programs from Java compilers, which always produce verifiable code. Therefore, the error messages they emit when encountering a class that fails verification can be cryptic. Your best bet is to trace the code as described in this chapter, drawing stack pictures and checking that each instruction receives the correct arguments. Also ensure that local variable and local stack limits are set high enough. (Running javap -verbose will tell you these limits.)

If all else fails, try writing the equivalent of the class in Java, and then disassembling the resulting class file using the Gnoloo disassembler. The differences between the class you wrote by hand and the one produced by the Java compiler may be revealing.

Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net