How to dynamically create instance of .class

A

andrewzzz

Hi sir...
I am working(more properly studying) on the server side of a
distributed system.
The server receives correctly a .class file and write it correctly to
a file.
At this point the server should create a new instance of the class :
so I redefinied the default classloader ("AgentClassLoader") that
correctly creates the class,still not instaciated.
Now I "think" I should use reflection to start theclass....but I don't
know how....I'm not very fond reflection!!
Note that this class is a thread with only the run and has a
constructor...so it should be very simple...How do I (what is the
simplest way to) instanciate the class....?? I do I create the
constructor?? How do I call the start or run method of my thread?
(should I call run() or start())?
Thank you so much.
andy
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

andrewzzz said:
I am working(more properly studying) on the server side of a
distributed system.
The server receives correctly a .class file and write it correctly to
a file.
At this point the server should create a new instance of the class :
so I redefinied the default classloader ("AgentClassLoader") that
correctly creates the class,still not instaciated.
Now I "think" I should use reflection to start theclass....but I don't
know how....I'm not very fond reflection!!
Note that this class is a thread with only the run and has a
constructor...so it should be very simple...How do I (what is the
simplest way to) instanciate the class....?? I do I create the
constructor?? How do I call the start or run method of my thread?
(should I call run() or start())?

Object o = Class.forName("yourpackage.YourClass", true,
yourclassloader).newInstance();

Arne
 
M

Mark Jeffcoat

andrewzzz said:
Hi sir...
I am working(more properly studying) on the server side of a
distributed system.
The server receives correctly a .class file and write it correctly to
a file.
At this point the server should create a new instance of the class :
so I redefinied the default classloader ("AgentClassLoader") that
correctly creates the class,still not instaciated.
Now I "think" I should use reflection to start theclass....but I don't
know how....I'm not very fond reflection!!

If you can actually instantiate the class (and that's how you'd
do it; build your own Classloader to load from a file), then you
can use Class.newInstance() to create a new instance with the
default (no-arg) constructor. Class.getConstructor(...).newInstance()
is there if you need more flexibility.

constructor?? How do I call the start or run method of my thread?
(should I call run() or start())?

Once you have the class instantiated, i.e.,
Runnable r = (Runnable) loadedClass.newInstance();

there's nothing special about it. Use it like you'd use any
other Runnable. (Or Thread, if that's what you meant.)

I elide the details to force you to go find a more complete
threading tutorial if you don't already know how to do that,
since you're otherwise about to get into trouble.
 
A

andrewzzz

If you can actually instantiate the class (and that's how you'd
do it; build your own Classloader to load from a file), then you
can use Class.newInstance() to create a new instance with the
default (no-arg) constructor. Class.getConstructor(...).newInstance()
is there if you need more flexibility.


Once you have the class instantiated, i.e.,
Runnable r = (Runnable) loadedClass.newInstance();

there's nothing special about it. Use it like you'd use any
other Runnable. (Or Thread, if that's what you meant.)

I elide the details to force you to go find a more complete
threading tutorial if you don't already know how to do that,
since you're otherwise about to get into trouble.

Ok , So 've tried in many ways (forname,getinstance...) but I can't
create an instance and run this very simple thread class...
Now I'll post my code...

Server class :

//is very simple...
AgentClassLoader loader = new AgentClassLoader();//creates instance
of my redefinied classloader

//Load the main class through agentclassloader

Class class=loader.loadClass(AGENT_NAME,FILE_PATH,false);

//I won't post my classloader code because it works correctly



System.out.println("Now Launching Agent..");
try {

class = Class.forName("app.MyTestAgent", true,loader);//I tryied
these methods..for name and new instance....but without success

//Note : "app" is the package and MyTestAgent is the class I want to
instanciate

class.newInstance();

} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}





2nd Class : "MyTestAgent" extend Thread and is veeery simple!!



public class MyTestAgent extends Thread {

Platform platform;//this is a simple collector class with only simple
data inside
public MyTestAgent (Platform platform) {
this.platform = platform;
}
public void run(){//I have to run this method!!

//I won't post this code because is very simple
(retrieves parameters from plaform object)


}




Anybody knows how to do it??
Please Help Me!!
Thanks a lot guys ... bye..
 
M

Mark Jeffcoat

andrewzzz said:
Class class=loader.loadClass(AGENT_NAME,FILE_PATH,false);

//I won't post my classloader code because it works correctly

try {

class = Class.forName("app.MyTestAgent", true,loader);//I tryied
these methods..for name and new instance....but without success

//Note : "app" is the package and MyTestAgent is the class I want to
instanciate

class.newInstance();

You know you have to do both, right? forName and newInstance.

class = Class.forName("app.MyTestAgent");
MyTestAgent = (MyTestAgent) class.newInstance();

} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}

If you're getting an exception here, you should,
you know, read it. If the problem is that you don't
understand the exception, and you want an explanation,
you'll have to post the actual text.


Can you write a similar program that uses the default
classloader, and works? Make sure you can do that first,
then try with your own classloader. If you can't, you'll
at least have a simpler problem to solve.
 
T

Thomas Fritsch

andrewzzz said:
Ok , So 've tried in many ways (forname,getinstance...) but I can't
create an instance and run this very simple thread class...
Now I'll post my code...
[...]
Class class=loader.loadClass(AGENT_NAME,FILE_PATH,false);
You can't use "class" as your variable name, because "class" is already a
key-word of the Java language. Use "c" or something else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top