首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jython将参数从Java传递给Python

使用Jython将参数从Java传递给Python
EN

Stack Overflow用户
提问于 2018-08-06 21:11:05
回答 1查看 2.2K关注 0票数 1

我使用Jython将字符串值传递给python脚本。在我的节目里,我做了好几次。但是,在我运行的测试中,为了查看发送参数的类是否正常工作,我看到python脚本输出的字符串值与初始输入相同。

下面是Java类:

代码语言:javascript
复制
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脚本:

代码语言:javascript
复制
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的输出是第一个数组输入的重复。任何帮助都是非常感谢的。

我得到以下输出:

代码语言:javascript
复制
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!!

我希望:

代码语言:javascript
复制
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!!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-02 15:29:36

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

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

interp.execfile("pyscript.py");替换为如下所示:

代码语言:javascript
复制
interp.exec("from pyscript import pyscript");
PyObject func = interp.get("pyscript");
func.__call__(new PyString(start), new PyString(end), new PyString(subject));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51715687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档