From: David B. <dav...@go...> - 2008-11-30 00:23:10
|
Hi there, I am new to using jython and im having trouble with the basics. I have tried to embed simple python programs in java, as in some tutorials on jython have shown but im constantly getting syntax errors. If i try something like this, import org.python.core.PyException; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.util.PythonInterpreter; public class SimpleEmbedded { public static void main(String[] args) throws PyException { PythonInterpreter interp = new PythonInterpreter(); interp.exec("import sys"); interp.exec("print sys"); interp.set("a", new PyInteger(42)); interp.exec("print a"); interp.exec("x = 2+2"); PyObject x = interp.get("x"); System.out.println("x: " + x); } } I get an error, Traceback (innermost last): (no code object) at line 0 File "simpleEmbed.java", line 6 public class Simple Embedded { ^ SyntaxError: invalid syntax Next trying something like this, import org.python.util.PythonInterpreter; import org.python.core.*; class TestPythonTwo { public static void main ( String[] args ) { try { org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter(); python.exec ( "import math" ); python.exec ( "print 'Hello Jython World!'" ); python.exec ( "print math.pi" ); python.exec ( "print math.e" ); python.exec ( "print math.sqrt ( 25 )" ); } catch ( Exception e ) { System.out.println ( "An error was encountered." ); } } } gives an error, Traceback (innermost last): (no code object) at line 0 File "embedTestPython.java", line 2 import org.python.core.*; ^ SyntaxError: invalid syntax Same error but its pointing to the * instead of class. I think this has something to do with the import lines, as if i change the, import org.python.core.*; line to, import org.python.core.__builtin__; it will give an error pointing to the first curly bracket { in the code. Anyone know what is causing it? Im sure its very simple but Im new to both java and jython as of a few weeks ago and really struggling with this. Many thanks for any help. |