Pattern Extraction using java.lang.reflect

Hello.

I'm working on a project where I need to create Class objects out of all of the .class files in a directory and in any subdirectory. I'm trying to use the Class.forName() method.

My problem is that I'm not able to pass a path to the .class file in the Class.forName() method. But I don't want to have to copy all of the .class files into the same directory as the code I'm using to extract the information I need.

Any tips on making Class.forName() work with a path to the .class file would be greatly appreciated.

-Crystal.

[567 byte] By [crys.redmana] at [2007-10-2 20:15:22]
# 1

> Hello.

>

> I'm working on a project where I need to create Class

> objects out of all of the .class files in a directory

> and in any subdirectory. I'm trying to use the

> Class.forName() method.

You don't understand how the class loader works.

> My problem is that I'm not able to pass a path to the

> .class file in the Class.forName() method.

Right, because the class loader is looking for classes in the CLASSPATH you specified, not "paths".

> But I

> don't want to have to copy all of the .class files

> into the same directory as the code I'm using to

> extract the information I need.

You don't have to. If those other packages are in the CLASSPATH the class loader will find them.

> Any tips on making Class.forName() work with a path

> to the .class file would be greatly appreciated.

Yes - get a better understanding of how the class loader works.

%

duffymoa at 2007-7-13 22:57:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

easy. you need to write your own classloader. have your classloader walk through the directory of your choice, loading each class file in as an array of bytes which you then pass to the protected method defineClass(String name, byte[] buf, int start, int length)

this method will return a Class object, providing you've done everything correctly

georgemca at 2007-7-13 22:57:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
So, every time I want to look at a different directory of .class files, I have to change the CLASSPATH?-Crystal.
crys.redmana at 2007-7-13 22:57:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Every time you want to look at a different directory hierarchy of class files you have to add that directory to the classpath used by your program.

Class files can be organised into a package hierarchy - they do not all have to be in one directory, and not all of the directories in the hierarchy have to be in the classpath used by the program.

The CLASSPATH environment variable is not the recommended way to set the classpath used by your program. Prefer the -classpath flag on the command line. For example:

java -classpath c:\foo com.paperstack.Foo

Implies that there is a class file:

c:\foo\com\paperstack\Foo.class

That the first line of the source code for this class reads:

package com.paperstack;

And that the class contains a method declared thus:

public static void main(String[] args) { ... }

Please read up on the classpath in the Java tutorials. It is extremely important that you understand the distinction between the classpath, the class name, and the directory containing the class file. If you don't understand all of these first you will find it impossible to reliably run your programs. If you do learn the distinctions you will find it trivially easy.

dcmintera at 2007-7-13 22:57:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

What I need to do for this project is turn a .class file into a Class object. I'm not interested in actually loading an instance of the class that the Class object represents. I just want to create the Class object so I can call methods such as getDeclaredFields() on that Class object. I'm only interested in using the Class class to create a feature set about the system under analysis so that I can play with some pattern extraction algorithms. Do I still need a ClassLoader for this?

Thanks.

-Crystal.

crys.redmana at 2007-7-13 22:57:39 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
You could take a look at Asm and CGLIB. Using a classloader is by far the easiest approach, however.
dcmintera at 2007-7-13 22:57:39 > top of Java-index,Other Topics,Patterns & OO Design...