import org.python.util.PythonInterpreter;
public class JythonTest {
public static void main(String[] args) {
PythonInterpreter interp = new PythonInterpreter();
interp.exec("if 2 > 1:");
interp.exec(" print('in if statement!'");
}
}
我需要能够从 Java 程序执行 Python 代码,因此决定尝试 Jython,但我对它不熟悉。我尝试执行上面的代码,但收到错误:“线程“main”中的异常 SyntaxError: (“不匹配的输入 '' 期待 INDENT”, ('', 1, 9, 'if 2 > 1:\n')) ”。任何想法这意味着什么或者我如何使用 PythonInterpreter 执行 if 语句?
最佳答案
条件必须作为单个字符串输入,并且有一个额外的括号:
import org.python.util.PythonInterpreter;
public class JythonTest {
public static void main(String[] args) {
PythonInterpreter interp = new PythonInterpreter();
interp.exec("if 2 > 1: print 'in if statement!'");
}
}
关于java - 如何使用 Jython 执行 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265094/