Mike Deck
Mike Deck

Reputation: 18397

Create a BCEL JavaClass object from arbitrary .class file

I'm playing around with BCEL. I'm not using it to generate bytecode, but instead I'm trying to inspect the structure of existing compiled classes.

I need to be able to point to an arbitrary .class file anywhere on my hard drive and load a JavaClass object based on that. Ideally I'd like to avoid having to add the given class to my classpath.

Upvotes: 5

Views: 2268

Answers (3)

JITHIN_PATHROSE
JITHIN_PATHROSE

Reputation: 1406

The existing .class can be class loaded to java lang class object. Then it can be converted into BCEL intermediate javaclass structure. The following code may help:-

Class<?> javaClass1 = null;
javaClass1 = ucl.loadClass("com.sample.Customer");
org.apache.bcel.classfile.JavaClass javaClazz1=org.apache.bcel.Repository.lookupClass(javaClass1);

Upvotes: 3

ILOVEPIE
ILOVEPIE

Reputation: 167

new ClassParser(classfilebytearrayhere).parse()

Upvotes: 2

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

The straightforward way is to create a ClassParser with the file name and call parse(). Alternatively you can use SyntheticRepository and supply a classpath (that is not your classpath, IYSWIM).

Upvotes: 11

Related Questions