From: Robert W. B. <rb...@di...> - 2001-08-21 16:53:34
|
On Tue, 21 Aug 2001, Young-Jin Lee wrote: > Hi, all. > I have a question on the imbedded Jython. It seems like that a Java > application can use a Jython as an interpreter module, but I'm not sure > what kind of things can be done with the imbedded Jython. Anything you can do in Jython can be done with the embedded interpreter. > For example, > can a Java application call a user-defined Python method through a > Jython interpreter? Yes, with ease. > If it cannot be done, is there any other way to do > this? Using the embedded interpreter is best. > I need a Java interpreter module.... Here's an example... import java.util.Properties; import org.python.util.PythonInterpreter; public class embeddingExample { protected PythonInterpreter interp; public static void main(String[] args) { embeddingExample embed = new Embedding(); embed.startInterp(); embed.test(); } protected void startInterp() { Properties props = new Properties(); if (System.getProperty("python.home") == null) props.put("python.home", "/usr/local/jython"); PythonInterpreter.initialize(preProps, postProps, argv); interp = new PythonInterpreter(); } public void useInterp() { // define and call a Jython function interp.exec("def test(message):\n" + " print message"); interp.exec("test('Hello world')"); // you can import Python modules as use them just as easily interp.exec("import glob\n" + "print glob.glob('*')"); } } Enjoy, Robert |