Jpython Question

Randy Hudson rgh at inmet.com
Fri Jul 23 13:00:11 EDT 1999


Ian Kezsbom wrote:
> 
> Just out of curiosity...lets say I've written a Java program, with many
> classes and I want to run that program through JPython and have access to
> all its classes...how would I go about doing this...

I've done this; here's how:

First, define a thread that will become the main thread of the program.

  from java.lang import Thread, String
  from wherever import MyAppMainClass
  from jarray import array    # a method to convert a Python list to a Java array
  class AppThread(Thread):    # defines a class AppThread that extends java.lang.Thread
    def __init__(self,args):  # define the AppThread constructor
                              # This example takes the args list for the
                              # call to main as a constructor 
                              # "self" is the conventional ID for Java "this"
      self.args = args
    def run(self):            # override  Thread.run()
      MyAppMainClass.main( array(self.args,String) )

At this point we've define a class AppThread to run the program on another
thread. Create an instance of this thread, with the appropriate "command line"
arguments:

  appThread = AppThread(["-runSilent", "-runDeep"])

If you need to set any Java properties, do it now:

  from java.lang import System
  props = System.getProperties()
  props['maxDepth'] = '20000'    # like a -DmaxDepth=20000 switch to jre

Now start the program

  appThread.start() # standard Thread.start(), which calls the run() defined above

At this point you can look at anything publicly accessible

  from wherever import InstrumentPanel
  nicely_display( InstrumentPanel.getDepth() )

-- Randy Hudson




More information about the Python-list mailing list