From: Young-Jin L. <yl...@ui...> - 2001-08-21 15:58:15
|
Hi, all.=20 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. For example, = can a Java application call a user-defined Python method through a = Jython interpreter? If it cannot be done, is there any other way to do = this? I need a Java interpreter module.... Thanks in advance. Young-Jin Lee |
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 |
From: Robert W. B. <rb...@di...> - 2001-08-21 17:47:56
|
The example I sent has at least 2 errors that need corrected. error 1- The "embed.test()" line should read "embed.useInterp()". error 2- the initialize arguments are wrong corrections noted below. sorry about that. On Tue, 21 Aug 2001, Robert W. Bill wrote: > > 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(); <-- ERROR embed.useInterp(); // <-- CORRECTION > } > > protected void startInterp() { > Properties props = new Properties(); > > if (System.getProperty("python.home") == null) > props.put("python.home", "/usr/local/jython"); //CORRECTED initialize method here PythonInterpreter.initialize(System.getProperties(), props, new String[0]); > 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('*')"); > > } > } |
From: Alan L. <al...@li...> - 2001-08-21 17:55:57
|
In addition to the use of interp.exec(), interp.eval() can be used to return values back to the Java side -- however you'll have to dig around a bit in the API docs for PyObject, PyInstance, PyTuple etc to learn how to get the underlying Java object out of the returned, wrapped, data. I've got Jython running a Java-based web server whose servlets (JSP) call into Jython to do dynamic evaluation of elements in a database which return Python objects to the servlets which then strip out the Java goodies to generate pages --- all works crisply and reliably. Alanl -----Original Message----- From: jyt...@li... [mailto:jyt...@li...]On Behalf Of Robert W. Bill Sent: Tuesday, August 21, 2001 9:53 AM To: Young-Jin Lee Cc: jyt...@li... Subject: Re: [Jython-users] [Q] newbie's question on Jython 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 _______________________________________________ Jython-users mailing list Jyt...@li... http://lists.sourceforge.net/lists/listinfo/jython-users |