java - 使用 Jython 将参数从 Java 传递到 Python

标签 java python jython jyni

我正在使用 Jython 将字符串值传递给 python 脚本。在我的程序中,我多次这样做。然而,在测试期间,我正在运行以查看发送参数的类是否正常工作,我发现 python 脚本输出与初始输入相同的字符串值。

这是 Java 类:

public class SendToCal
{


PythonInterpreter interp  = null;
StringWriter clout = null;
String [] arguments = null;

private String start=null,end=null,subject = null;
Properties props = new Properties();


public SendToCal(String start,String end, String subject) 
{
    try{

    setInputs(start,end,subject);
    arguments = getInputs(start,end,subject);
    //---------------------------------------------
    //---------------trying out jython test--------
    props.setProperty("python.path","C:\\folder\\where\\I\\have\\stuff\\2.7-b1\\Lib', '__classpath__', '__pyclasspath__/");
    PythonInterpreter.initialize(System.getProperties(), props,arguments);


    this.interp = new PythonInterpreter();

    clout = new StringWriter();

    interp.setOut(clout);
    interp.execfile("C:\\folder\\to\\file\\pyscript.py");


    String outputStr = clout.toString();

    System.out.println(outputStr);
    clout.close();
    }catch(IOException ex)
    {
        ex.printStackTrace();
    }



}
 public void setInputs(String start, String end, String sub)
{
    this.start = start;
    this.end = end;
    this.subject = sub;

}

public String[] getInputs(String start, String end, String sub)
{
    String [] arr = {this.start,this.end,this.subject};

   return arr;

}


public  static void main(String[] args) throws InterruptedException
{

    String st2 = 2018+"-"+ 8 +"-"+ 6+ " " +"12:00";
    String en2 = 2018+"-"+ 8 +"-"+ 6+ " " +"11:59";
    String su2 = "YAY PYTHON AGAIN!!";
    new SendToCal(st2, en2, su2);

    TimeUnit.SECONDS.sleep(1);


    String st = 1999+"-"+ 7 +"-"+ 17+ " " +"12:00";
    String en = 1999+"-"+ 7 +"-"+ 17+ " " +"11:59";
    String su = "YAY PYTHON!!";
    new SendToCal(st, en, su);


 }
}

此外,下面是我的 python 脚本:

import sys
def pyscript(arg1,arg2,arg3):
    print ("calling python function with parameters:")
    print arg1
    print arg2
    print arg3

pyscript(sys.argv[0],sys.argv[1],sys.argv[2])

我的问题是,当我使用两个或多个完全不同的字符串数组调用 Java 构造函数时,python 的输出是第一个数组输入的重复。如有任何帮助,我们将不胜感激。

我得到以下输出:

calling python function with parameters:
2018-8-6 12:00
2018-8-6 11:59
YAY PYTHON AGAIN!!

calling python function with parameters:
2018-8-6 12:00
2018-8-6 11:59
YAY PYTHON AGAIN!!

我期望:

calling python function with parameters:
2018-8-6 12:00
2018-8-6 11:59
YAY PYTHON AGAIN!!

calling python function with parameters:
1999-7-17 12:00
1999-7-17 11:59
YAY PYTHON AGAIN!!

最佳答案

PythonInterpreter.initialize()方法应该只被调用一次。它在您的程序中被调用两次,第二次时,sys.argv 未更新。

解决这个问题的方法是使用 pyscript.py 作为模块并调用模块中定义的函数。

interp.execfile("pyscript.py"); 替换为如下内容:

interp.exec("from pyscript import pyscript");
PyObject func = interp.get("pyscript");
func.__call__(new PyString(start), new PyString(end), new PyString(subject));

关于java - 使用 Jython 将参数从 Java 传递到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51715687/

相关文章:

python - 在 Python 中对角镜像

groovy - Groovy优于Jython或Jruby?

java - 如何在 JavaFX 中的 Accordion 中的 TitledPanes 之间添加填充

java - 有商业/开源 Swing 替代方案吗?

python - 在 PIL 中绘制半透​​明多边形

python - 为什么 .readlines() 会生成单个字符的列表?

java - 通过构造函数进行依赖注入(inject)中的循环依赖

java - Excel 显示 : "Excel file is not a valid file extension or format type..."

python - 必须使用 x 实例作为第一个参数调用未绑定(bind)方法 f() (改为使用 str 实例)

java - 为什么这个 Jython 循环在一次运行后就失败了?