This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to create a class definition at runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to create a class definition at runtime?" Watch "How to create a class definition at runtime?" New topic
Author

How to create a class definition at runtime?

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
How to create a class definition at runtime?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

At runtime, the class is loaded and not defined. There is no class definer, there is only class loader.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

What do you mean exactly by "creating a class definition at runtime", can you explain your question in more detail?

There are libraries that can help you to programmatically create class files and byte code, for example Apache BCEL (which stands for Byte Code Engineering Library). But this is very advanced and something that's only necessary for very specialistic applications. It's certainly not something that belongs in the "Beginning Java" forum...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
Class A
{

}

Class Is not designed before I Run my code but It should be created by running code and dynamically I will create a Object from that class
?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Two possible approaches are: a) Use a library like Jesper suggested. Instead of BCEL I'd go with Javassist, though, which makes it easy to create classes from code fragments.

b) Write the code to a ".java" file, compile it (there's an API for that in Java 6), and put the classes into the classpath of the application.


Ping & DNS - my free Android networking tools app
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
is it jesper or jasper?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

I'm pretty sure Jesper knows how to spell his own name.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well as Jesper said there are libraries for java to create the class files. Well i suppose when i had studied Programming with Components i.e COM there was something similar to that which created some files based on the signatures, but i think that is not at runtime. Its still during compiling, the class files are created after a successful compilation. May be i have forgot many things on that as it was some couple of years back. Just clarify on this please.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to create a class definition at runtime?
 
Similar Threads
Is it possible to extend a final class? (With byte code engineering or whatever)
Reading blocks of data from files
how to define comparator myself?
<jsp:UseBean> can also CREATE a bean! Or can it?
Parent and child relationship configuration in spring?