• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to create a class definition at runtime?

 
Ranch Hand
Posts: 588
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create a class definition at runtime?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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: 588
  • 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
?
 
Rancher
Posts: 43016
76
  • 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: 588
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it jesper or jasper?
 
Marshal
Posts: 67431
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 Python Java
  • 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.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic