Results 1 to 20 of 20
  1. #1
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default After loading class, how to run it?

    Here is my situation:
    I created an abstract class that implements Runnable. Other people who make "scripts" for this will extend this abstract class, and compile it. Here is the the abstract class:
    Java Code:
    package yoctoscript;
    
    import com.yoctobot.api.methods.Methods;
    
    public abstract class Script extends Methods implements Runnable, Paint {
    
    	public boolean paused = false;
    
    	public abstract void start();
    
    	public abstract int mainCycle();
    
    	public abstract void onPause();
    
    	public abstract void onStop();
    
    	public void run() {
    
    		while (true) {
    			if (paused)
    				continue;
    			try {
    				Thread.sleep(mainCycle());
    			} catch (InterruptedException p) {
    
    			}
    		}
    	}
    }
    I have created a class loader, so All that I would have to do is:
    Java Code:
    Class script1 = ClassLoader.loadScript();
    After I have loaded it, How would I run the loaded class through my program? Thanks! (And sorry for asking lots of question lately :( )

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    How would I run the loaded class
    You 'run' code by call a method.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    As Norm already wrote: you don't run a class, you call methods from that class. When you know nothing about the class you have to use dirty tricks like Reflection and such to find a method you can call but you know that an object of that class implements the Runnable interface so it has a run() method. Cast an instance of that class to the Runnable interface and call the run() method.

    kind regards,

    Jos

  4. #4
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Okay, Now suppose I have loaded the class (it is stored in Class c), now what should I do?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    Okay, Now suppose I have loaded the class (it is stored in Class c), now what should I do?
    Create a new instance? Read all about it in the API documentation for the Class class.

    kind regards,

    Jos

  6. #6
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Thanks for your reply,
    I can you give me an example? I am still confused. I couldn't find anything online about it. And how do you create an instance? Thanks!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    Thanks for your reply,
    I can you give me an example? I am still confused. I couldn't find anything online about it. And how do you create an instance? Thanks!
    That class has a newInstance() method (as you could've read in the API documentation). Cast that instance to a Runnable and you're in business.

    kind regards,

    Jos

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by cselic View Post
    Here is a link with examples of programs that use threads.
    http:/http://www.isr.umd.edu/~austin...d/threads.html
    For one thing: that link doesn't work; for another thing: what have threads got to do with this? Did you actually read this thread?

    kind regards,

    Jos

  9. #9
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Java Code:
    Class clazz = SSClassLoader.getSystemClassLoader().loadClass("MouseMover");
            Runnable t  = (Runnable) clazz.newInstance();
            t.run();
    Sweet! It works now :-)
    But lets say I want to pause it. How should I do this? Many,many thanks!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    Java Code:
    Class clazz = SSClassLoader.getSystemClassLoader().loadClass("MouseMover");
            Runnable t  = (Runnable) clazz.newInstance();
            t.run();
    Sweet! It works now :-)
    But lets say I want to pause it. How should I do this? Many,many thanks!
    That run() method runs in the same thread as all your other code; please reformulate your question more exact.

    kind regards,

    Jos

  11. #11
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Yes. How can i stop this? Lets say I make "t" a new Thread. I invoke t.run() to run it, but so stop it, t.stop() is deprecated. what now?

  12. #12
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Other question. Lets Say I want to invoke the Graphics method. How should I do it?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    Other question. Lets Say I want to invoke the Graphics method. How should I do it?
    You have to be more clear; what is a 'Graphics method'? A visual component can draw itself starting at the paintComponent( ... ) method ...

    b.t.w. (related to your previous reply), you start a thread with its start() method, not its run() method.

    kind regards,

    Jos

  14. #14
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Oh okay. So Instead of having it Thread.sleep(mainCycle()) you have it Thread.sleep(start())?

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    Oh okay. So Instead of having it Thread.sleep(mainCycle()) you have it Thread.sleep(start())?
    Do the methods start() and mainCycle() return a long? Because that's the type of the parameter for the sleep( ... ) method. I don't understand your question.

    kind regards,

    Jos

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    but to stop it,
    One way would be to have an interface that the Scripts implement that you call to tell the Script to end and return.

    BTW you'll also want a Security manager to prevent the Script from calling System.exit();

  17. #17
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    One way would be to have an interface that the Scripts implement that you call to tell the Script to end and return.

    BTW you'll also want a Security manager to prevent the Script from calling System.exit();
    Ah thanks Norm; that did the job. As for the security manager, I am reading on it now. For the most part, we can mark this thread solved.

  18. #18
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    new problem. I run the the script, and the my applet is un - clickable,

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    my applet is un - clickable
    What does that mean?
    Where does "applet" come into this?

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by crikey View Post
    new problem. I run the the script, and the my applet is un - clickable,
    A new problem is best asked in a new thread and you'll want to post enough information so that it is answerable. Luck.

Similar Threads

  1. Replies: 8
    Last Post: 04-14-2010, 06:49 AM
  2. Dynamic Class Loading from external Jar
    By AndreB in forum Advanced Java
    Replies: 3
    Last Post: 02-08-2010, 02:55 PM
  3. class loading
    By purejoker in forum AWT / Swing
    Replies: 1
    Last Post: 01-20-2009, 01:09 PM
  4. Factory loading class...help?
    By prabhurangan in forum Advanced Java
    Replies: 2
    Last Post: 10-14-2008, 03:20 PM
  5. Help with class loading in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •