Running jython bytecode using java

Looks like I missed something.

When using Jython to run my Python code in Java, Java bytecode files are generated (test.py -> test@py.class).

Is it possible to run these classes directly using java?

In other words, I want to do this:

$ java test@py [additional cp args] 

Work.

Purpose: To write Python code and not pass the source code.

+9
java python jython bytecode
Apr 6 2018-10-06T00:
source share
3 answers

Here is what works for me:

test_p.py:

 def foo(): print 'test from Python' 

TestJ.java:

 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<?> 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(); } } } 

Compile test_p.py into test_p $ py.class:

 $JYTHON_HOME/jython $JYTHON_HOME/Lib/compileall.py . 

Modify test_p.py to prove that it is not used:

 mkdir hidden mv test_p.py hidden/ 

Compile:

 javac -cp $JYTHON_HOME/jython.jar TestJ.java 

Test:

 java -cp $JYTHON_HOME/jython.jar:. TestJ 

Output:

 test from Python 
+4
Apr 11 '13 at 0:48 on
source share

See the FAQ - Embedding Jython .

Please note that jythonc no longer supported :

jythonc does not process generators and is difficult to debug and improve. The current thinking is to add capabilites to jython itself to generate bytecode from py files and run those statically compiled elements, rather than the jythonc approach, when Java classes work like Python base code. Current thinking works as follows:

  • Turn Python classes into Java classes without an interface or Java class, using function annotations to specify static information such as Java.
  • statically compile proxy classes for Python classes extending Java classes
  • remove code from the kernel that exists only for jythonc support

This example offers a special annotation for any method in the Python class that should be visible with Java:

 class Simple(object): @java def __init__(self): @java(String, String) def firstWord(self, param): return param.split(' ')[0] 
+3
Apr 6 '10 at 7:20
source share

If your problem is only with distributing your application without giving away the source, you can look at tools such as cx_freeze and py2exe on Windows and py2app on Mac.

These tools have the ability to compile .py files into bytecode.

0
Apr 6 2018-10-06T00:
source share



All Articles