est ici ce qui fonctionne pour moi:
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 dans test_p py.class de $ :
$JYTHON_HOME/jython $JYTHON_HOME/Lib/compileall.py .
Déplacer test_p.py de la route, pour le prouver n'est pas utilisé:
mkdir hidden
mv test_p.py hidden/
Compile:
javac -cp $JYTHON_HOME/jython.jar TestJ.java
Test:
java -cp $JYTHON_HOME/jython.jar:. TestJ
Sortie:
test from Python
Le compilateur pypy est probablement un meilleur pari d'un point de vue obfuscation. Malheureusement, c'est très difficile à utiliser. – Antimony