Get class bytes? (Java)
Well, as much as I hate to crosspost, this question only seemed to provoke confusion from the folks at the Java Developers Network Forum. Basically, after loading some class (which could of course reside on a remote machine, inside an archive, etc) through a ClassLoader object, I then need to retrieve the raw .class (format) data. The Class class appears to only provide indirect means to access the data, but what I really need is the entire .class file.
Any idea what API would accomplish this?
Hmm... I don't think there is a straight-forward way of doing this. What you may be able to do is replace the System ClassLoader with your own class. The System ClassLoader is the class which loads all the user defined classes from the CLASSPATH. You can read more about it here. I don't know if this would give you access to the raw .class file bytes though.
You could try something like this:
public static byte getClassBytes(Class clazz) throws IOException
String name = clazz.getName().replace('.', '/') + ".class";
InputStream iStream = clazz.getClassLoader().getResourceAsStream(name);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
Please, put the code snippet in the code tags.
Nathan Paulino Campos
Originally Posted by sdaubin