Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a given class (let's say, java.lang.Object) to a byte array. Is it possible?

Given that class loaders accept to take as input a byte array of a given class, returning a Class<?>, I wonder whether it is possible to do the reverse, that is, to pass a Class<?> and get its byte array?

Keep in mind that I'm not talking about serialization!

like image 467
devoured elysium Avatar asked Feb 23 '23 12:02

devoured elysium


1 Answers

You can use ClassLoader.getResourceAsInputStream() however it is not guaranteed this will be the same bytes as that which was loaded. I don't believe the actual bytes loaded are stored anywhere.

like image 121
Peter Lawrey Avatar answered Apr 27 '23 02:04

Peter Lawrey