aspose file tools*
The moose likes Java in General and the fly likes Defining classes runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Defining classes runtime" Watch "Defining classes runtime" New topic
Author

Defining classes runtime

Yohan Liyanage
Ranch Hand

Joined: Aug 17, 2007
Posts: 132

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
http://blog.yohanliyanage.com
Yohan Liyanage
Ranch Hand

Joined: Aug 17, 2007
Posts: 132

anybody?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Why would you want to create a new class at runtime - why do you think that would be a useful feature?


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

Joined: Dec 01, 2005
Posts: 99
Are you asking for auto generated files??

somewhere i read java file can created , compiled and executed at runtime using scripting languages


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
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

Joined: Aug 17, 2007
Posts: 132

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
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
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).


Ping & DNS - my free Android networking tools app
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
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

Joined: Nov 29, 2005
Posts: 2308
Originally posted by harish thrivile:


yes, you can dynamically extend java classes


how ? and useful link for this.
Yohan Liyanage
Ranch Hand

Joined: Aug 17, 2007
Posts: 132

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
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
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

Joined: Nov 29, 2005
Posts: 2308
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Defining classes runtime