From: Robert W. B. <rb...@di...> - 2001-08-20 16:14:04
|
Hello Pulak, On Mon, 20 Aug 2001, Pulak Piplani (UAB) wrote: > Hi, > > I am tring to use Jython for a scripting application to configure a External Device. > > The idea is to provide user the flexibility of writing scripts in jython while the GUI and other aspects of external communications etc are handled in Java. > > Once the GUI is ready and the communication class is instantiated the control will be passes to a jython script. > > The communication Object ( which is a java object) will be passes on to the Jython script which will then call "Send(Command)" interface for example. > > > To do I am doing the following > > Java side > -------------- > //jptComm object is already instantiated and setup. It has an interface send(string). > > PythonInterpreter interp = new PythonInterpreter(); > try{ > interp.execfile(fileName); // where filename is the name of jython script ( runscript.py) > } > catch( PyException except) > { > System.out.println("Error occured in exec file"); > } > try{ > interp.exec("run(jptComm)"); // where the jptComm in the communication object. How did the interpreter get the identifier "jptComm"? Maybe a "set" is missing- see end. > //interp.exec("run(2)"); //instead if I pass builtin types like int or string it works fine. > } > catch( PyException except) > { > System.out.println("Error occured in EXEC"); > System.out.println(except.toString()); > } > } > __________________________________________________________________________________ > > > Jython side > --------------- > > > import java > import jpitool.jpitoolCommunicator > > def run(jcp): > print 'xyz' > #jcp.send('ls') > > > if __name__ == '__main__': > run(2) Still no jptComm identifier set. This is starting to look suspicious. <snip> > Error occured in EXEC > Traceback (innermost last): > File "<string>", line 1, in ? > NameError: jptComm I'm usure what object jptComm is, but here's my guesses: 1. jptComm is an object that should be created in the Jython script- maybe an instance of jpitoolCommunicator. If this is the case, change the Jython code to the following: =================================== import java import jpitool.jpitoolCommunicator jptComm = jpitool.jpitoolCommunicator(someargs) # <--create jptComm def run(jcp): print 'xyz' if __name__ == '__main__': run(2) =================================== 2. jptComm is a Java object that did not get set in the interpreter. If this is the case, add a "set" method previous to the call to interp.exec("run(jptComm)") like the following: interp.set("jptComm", myjptCommObject); interp.exec("run(jptComm)"); either way, jptComm is defined before you use it in the run function, which is the fix. Cheers, Robert |