Results 1 to 20 of 20
- 08-22-2010, 02:28 AM #1
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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:
I have created a class loader, so All that I would have to do is: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) { } } } }
After I have loaded it, How would I run the loaded class through my program? Thanks! (And sorry for asking lots of question lately :( )Java Code:Class script1 = ClassLoader.loadScript();
- 08-22-2010, 03:33 AM #2
You 'run' code by call a method.How would I run the loaded class
- 08-22-2010, 09:05 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-23-2010, 07:10 PM #4
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Okay, Now suppose I have loaded the class (it is stored in Class c), now what should I do?
- 08-23-2010, 07:35 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-23-2010, 07:49 PM #6
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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!
- 08-23-2010, 07:56 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-23-2010, 08:09 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-23-2010, 08:10 PM #9
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Sweet! It works now :-)Java Code:Class clazz = SSClassLoader.getSystemClassLoader().loadClass("MouseMover"); Runnable t = (Runnable) clazz.newInstance(); t.run();
But lets say I want to pause it. How should I do this? Many,many thanks!
- 08-23-2010, 08:13 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-23-2010, 09:14 PM #11
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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?
- 08-24-2010, 04:23 AM #12
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Other question. Lets Say I want to invoke the Graphics method. How should I do it?
- 08-24-2010, 08:02 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-24-2010, 04:07 PM #14
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Oh okay. So Instead of having it Thread.sleep(mainCycle()) you have it Thread.sleep(start())?
- 08-24-2010, 04:28 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-24-2010, 05:19 PM #16
One way would be to have an interface that the Scripts implement that you call to tell the Script to end and return.but to stop it,
BTW you'll also want a Security manager to prevent the Script from calling System.exit();
- 08-24-2010, 05:59 PM #17
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
- 08-25-2010, 10:16 PM #18
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
new problem. I run the the script, and the my applet is un - clickable,
- 08-25-2010, 10:18 PM #19
What does that mean?my applet is un - clickable
Where does "applet" come into this?
-
Similar Threads
-
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
Dynamic Class Loading from external Jar
By AndreB in forum Advanced JavaReplies: 3Last Post: 02-08-2010, 01:55 PM -
class loading
By purejoker in forum AWT / SwingReplies: 1Last Post: 01-20-2009, 12:09 PM -
Factory loading class...help?
By prabhurangan in forum Advanced JavaReplies: 2Last Post: 10-14-2008, 02:20 PM -
Help with class loading in java
By mathias in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:51 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks