여기에 나를 위해 작동 내용은 다음과 같습니다
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(); }
}
}
컴파일 test_p.py test_p의 $의 py.class로 :
$JYTHON_HOME/jython $JYTHON_HOME/Lib/compileall.py .
test_p로 이동하십시오.평 비켜, 사용하지 않을 것 증명하기 :
mkdir hidden
mv test_p.py hidden/
컴파일 :
javac -cp $JYTHON_HOME/jython.jar TestJ.java
테스트 :
java -cp $JYTHON_HOME/jython.jar:. TestJ
출력 :
test from Python
pypy 컴파일러는 아마도 난독 화 관점에서 더 나은 방법 일 것입니다. 불행히도, 사용하기가 매우 어렵습니다. – Antimony