Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what works for me:</p> <p><strong>test_p.py:</strong></p> <pre class="lang-py prettyprint-override"><code>def foo(): print 'test from Python' </code></pre> <p><strong>TestJ.java:</strong></p> <pre class="lang-java prettyprint-override"><code>import org.python.core.PyFrame; import org.python.core.PyFunctionTable; import org.python.util.PythonInterpreter; public class TestJ { public static void main(String[] args) { final PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("import sys"); try { final Class&lt;?&gt; clazz = Class.forName("test_p$py"); final java.lang.reflect.Constructor constructor = clazz.getConstructor(String.class); final PyFunctionTable module = (PyFunctionTable)constructor.newInstance(""); final java.lang.reflect.Method method = clazz.getDeclaredMethod("foo$1", PyFrame.class, org.python.core.ThreadState.class); method.invoke(module, (PyFrame)interpreter.eval("sys._getframe()").__tojava__(PyFrame.class), org.python.core.Py.getThreadState()); } catch (final ClassNotFoundException e) { e.printStackTrace(); } catch (final NoSuchMethodException e) { e.printStackTrace(); } catch (final InstantiationException e) { e.printStackTrace(); } catch (final IllegalAccessException e) { e.printStackTrace(); } catch (final java.lang.reflect.InvocationTargetException e) { e.printStackTrace(); } } } </code></pre> <p>Compile test_p.py into test_p$py.class:</p> <pre class="lang-csh prettyprint-override"><code>$JYTHON_HOME/jython $JYTHON_HOME/Lib/compileall.py . </code></pre> <p>Move test_p.py out of the way, to prove it's not being used:</p> <pre><code>mkdir hidden mv test_p.py hidden/ </code></pre> <p><strong>Compile:</strong></p> <pre class="lang-csh prettyprint-override"><code>javac -cp $JYTHON_HOME/jython.jar TestJ.java </code></pre> <p><strong>Test:</strong></p> <pre><code>java -cp $JYTHON_HOME/jython.jar:. TestJ </code></pre> <p><strong>Output:</strong></p> <pre class="lang-none prettyprint-override"><code>test from Python </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload