This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes loading class files from jar files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "loading class files from jar files" Watch "loading class files from jar files" New topic

loading class files from jar files

Patrick Cobbett
Ranch Hand

Joined: Jul 10, 2003
Posts: 44
Hello all,
I am trying to load class files within a jar file. The program extracts and iterates through all the ZipEntrys. For each ZipEntry i obtain the InputStream and read in the contents of each class file in to a byte array. I've made a ClassLoader which relaxes the access rights to the define method which take an array of bytes: Class defineClass(String name, byte[] data, int start, int end);
However only certain classes correctly return a Class objects. The majority of class files i try to read in this manner throws a ClassFormatException with one if the following messages:
Illegal constant pool type
Illegal UTF8 string in constant pool
Illegal constant pool index
Arguments can't fit into locals
any ideas?

sander hautvast
Ranch Hand

Joined: Oct 18, 2002
Posts: 71
you've probably done sth like:[] b)
like me...
This is what you should do:
new DataInputStream(inputStreamInstance).readFully(byte[] b)
I don't really know why because the apidoc for both methods looks the same. But it works!
[ May 06, 2004: Message edited by: sander hautvast ]
I agree. Here's the link:
subject: loading class files from jar files
It's not a secret anymore!