aspose file tools*
The moose likes Java in General and the fly likes generating classes dynamically.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "generating classes dynamically...." Watch "generating classes dynamically...." New topic
Author

generating classes dynamically....

deshki raja
Greenhorn

Joined: Apr 03, 2009
Posts: 12
Hi,
I want to generate classes dynamically based on the user inputs......is it possible in java ? Can anyone help me on this....?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

You need to clarify more.
What do you exactly mean by dynamic classes? Are you sure you want to mean classes and not objects?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
I moved this question to the Intermediate forum - this is definitely not a beginner's question :-)

One way of doing so is to use a library that can construct classes, like Javassist. I wrote an article about doing just that in the JavaRanch Journal a while ago.


Ping & DNS - my free Android networking tools app
deshki raja
Greenhorn

Joined: Apr 03, 2009
Posts: 12
what i meant was that...depending on the user inputs ....i want to create a class, generate the methods and create objects of the class...........
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Well then Ulf's link talks just exactly about that.
deshki raja
Greenhorn

Joined: Apr 03, 2009
Posts: 12
any other way to do this ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
What do you mean by "other way"? Is the suggested approach somehow not feasible in your situation? If so, tell us why it isn't.
ashishs Sinha
Greenhorn

Joined: Jul 01, 2008
Posts: 3
I guess you would be looking for two separate functionalities
1 - To generate class dynamically . You can generate java class dynamically using com.sun.codemodel pacakage.
This package provides lots of functionality to generate class programmatically - including adding method, extending/implementing other class and interface.

2 - To compile and generate class file or fetch error in runtime.
For this, you can use sun package - com.sun.tools.javac.Main to compile and to parse result or compiler error there are utility classes provided by apache in the package - org.apache.commons.jci.compilers and org.apache.commons.jci.problems

We are generating, compiling lot of classes dynamically based on user input. Later we are invoking the classes and storing the execution result in our database.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: generating classes dynamically....