Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Defining classes runtime

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody!

I would like to know if there is a possibility to create new classes and make use of them during the runtime of an application. I have heard about Java Reflection API, can it be used to solve my problem ?
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java reflection api is used for creating objects at rumtime , but the classes should be already available.It cannot create classes at runtime.It can load class , instantiate object and other related stuffs.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to create a new class at runtime - why do you think that would be a useful feature?
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking for auto generated files??

somewhere i read java file can created , compiled and executed at runtime using scripting languages
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, reflection gives you the methods for dynamically loading classes at runtime, and yes you can compile a class from within an application, so the parts are all there. It is not exactly simple.

You might find it a lot easier to use one of the scripting languages that can dynamically create scripts and be executed in the JVM. See Groovy, JPython, JRuby and probably a bunch more.

Bill
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody. I'll look into those scripting languages. I wanted to know if there is a possibility to dynamically extend a Java program.
[ August 27, 2007: Message edited by: Yohan Liyanage ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found the Javassist library very useful for this. Since it creates real Java classes, I'd think it would be much faster that anything that involves a scripting language (assuming that those are essentially interpreted, not compiled to bytecode).
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to know if there is a possibility to dynamically extend a Java program


yes, you can dynamically extend java classes
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by harish thrivile:


yes, you can dynamically extend java classes


how ? and useful link for this.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this article when I was searching the web for this problem.

Add dynamic Java code to your application By Li Yang :
http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
how ? and useful link for this.


As was said before, Javassist can do this. It comes with a tutorial that demonstrates how to do that.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:


As was said before, Javassist can do this. It comes with a tutorial that demonstrates how to do that.


Thanks , I will lookinto it.Wasn't aware of this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic