이 기사는 참조를 위해 Java Calling Python 방법을 공유합니다. 특정 내용은 다음과 같습니다
1. Java 클래스에서 Python 문을 직접 실행하십시오
import org.python.util.pythoninterpreter; public class firstJavaScript {public static void main (String args []) {pythoninterpreter Interpreter = new PythonInterpreter (); Interpreter.exec ( "days = ( 'mod', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');"); Interpreter.exec ( "인쇄 일 [1];"); }// 기본}통화 결과는 Tue이며 콘솔에 표시되며 직접 호출됩니다.
2. Java의 기본 파이썬 스크립트에서 기능을 호출합니다
먼저 My_utils.py라는 이름으로 Python 스크립트를 만듭니다
DEF 가산기 (A, B) : A + B를 반환합니다
그런 다음 테스트 할 Java 클래스를 만들고
Java 클래스 코드 FirstJavaScript :
import org.python.core.pyfunction; import org.python.core.pyinteger; import org.python.core.pyobject; import org.python.util.pythoninterpreter; public static void main (String args []) {pythoninterpreter = 새로운 Pythoninterpreter (); Interpreter.execfile ( "c : //python27//programs//my_utils.py"); pyfunction func = (pyfunction) interpreter.get ( "adder", pyfunction.class); int a = 2010, b = 2; pyobject pyobj = func .__ 호출 __ (New Pyinteger (a), new pyinteger (b)); System.out.println ( "anwser =" + pyobj.tostring ()); }// 기본}결과는 다음과 같습니다. Anwser = 2012
3. Java를 사용하여 Python 스크립트를 직접 실행하십시오
입력 스크립트를 만듭니다
#open 파일 인쇄 'hello'번호 = [3,5,2,0,6] 인쇄 번호 번호 번호 () 인쇄 번호 번호 번호 .append (0) 인쇄 번호 인쇄 번호 .count (0) 인쇄 번호 (5)
Java 클래스를 만들고이 스크립트를 호출하십시오.
import org.python.util.pythoninterpreter; public class firstJavaScript {public static void main (String args []) {pythoninterpreter Interpreter = new PythonInterpreter (); 통역사 .execfile ( "c : //python27//programs//input.py"); }// 기본} 결과는 다음과 같습니다.
안녕하세요 [3, 5, 2, 0, 6] [0, 2, 3, 5, 6] [0, 2, 3, 5, 6, 0] 2 3
위의 것은 Python Methods에 대한 세 가지 Java 전화입니다. 모든 사람의 학습에 도움이되기를 바랍니다.