Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

How to dynamically create instance of .class

Thread view: 
andrewzzz - 26 Feb 2007 22:31 GMT
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
Arne Vajhøj - 27 Feb 2007 00:13 GMT
> I am working(more properly studying) on the server side of a
> distributed system.
[quoted text clipped - 10 lines]
> 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
Mark Jeffcoat - 27 Feb 2007 01:13 GMT
> Hi sir...
> I am working(more properly studying) on the server side of a
[quoted text clipped - 6 lines]
> 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.

Signature

Mark Jeffcoat
Austin, TX

andrewzzz - 27 Feb 2007 23:50 GMT
> > Hi sir...
> > I am working(more properly studying) on the server side of a
[quoted text clipped - 29 lines]
> Mark Jeffcoat
> Austin, TX

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..
Mark Jeffcoat - 28 Feb 2007 14:56 GMT
> Class class=loader.loadClass(AGENT_NAME,FILE_PATH,false);
>
[quoted text clipped - 9 lines]
>
>        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.

Signature

Mark Jeffcoat
Austin, TX

Thomas Fritsch - 28 Feb 2007 16:00 GMT
> 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.

Signature

Thomas



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.