my dog learned polymorphism*
The moose likes Java in General and the fly likes Loading Java class files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading Java class files " Watch "Loading Java class files " New topic
Author

Loading Java class files

Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
HI ,

I want to get all the classes names which are located there and get information from class files.

I used Reflection to get all the fields information which is like this

However the above code is giving me correct result for the given exampleA.class . But I have some class files located under Users/ramu/NetBeansProjects/Ext/build/classes/poly/
1) I want to get all .class file names under specified directory
2)I want to pass class file location to getFieldsNames() so that I can reuse the same code to get information of all classes(Just by changing the parameter).

Any suggestions please
Thanks in advance

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Do you want the Java classes loaded? or just the files in the directory tree?

You can write (without too much work) a recursive tree walker for a directory, there is even one in my Java library on the web.
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Pat Farrell wrote:Do you want the Java classes loaded? or just the files in the directory tree?

You can write (without too much work) a recursive tree walker for a directory, there is even one in my Java library on the web.


All I want is to get the declared field names in every class .
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Again, you have to be more specific. What classes do you mean? Those loaded from your jar file? on the disk? In the whole JVM?
There are probably more types, but I can't remember them all right now.
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Pat Farrell wrote:Again, you have to be more specific. What classes do you mean? Those loaded from your jar file? on the disk? In the whole JVM?
There are probably more types, but I can't remember them all right now.


I mean the ones which are in my disk .
I have two packages in my project named "packageA" and "packageB" . This is a GUI application . When the user launches the application he will create a .java file and save it in a specified directory. Then user will compile the classes with in the gui and saves the .class file in a directory such as Ext/build/classes/poly/.

In "PackageB" , I need to load that .class file and get the information(just Declared filed names) form that .class file .

I tried this


I am getting an error
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Priyatham Anisetty wrote:I mean the ones which are in my disk .


Then I would skip the whole topic of the class loader, and classes that are in memory, and simply use the standard
Java functions to read the directory and get a list of the files. Filter to suit and you are done.

File and its listFiles() method are 99% of what you need.

You could use my DirWalker class from my open source Java library. http://www.pfarrell.com/java/pdflib.tar.gz
 
GeeCON Prague 2014
 
subject: Loading Java class files