Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a class definition at runtime?

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create a class definition at runtime?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At runtime, the class is loaded and not defined. There is no class definer, there is only class loader.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15287
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it jesper or jasper?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure Jesper knows how to spell his own name.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic