使用Jython将参数从Java传递到Python
我使用Jython将字符串值传递给python脚本。在我的程序中,我多次这样做。然而,在我运行的一个测试中,我看到发送参数的类是否正常工作,python脚本输出的字符串值与初始输入值相同 下面是Java类:使用Jython将参数从Java传递到Python,java,python,jython,jyni,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
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!!
该方法只能调用一次。它在程序中被调用两次,第二次,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));
代码不完整。没有import语句,在“hereisthejava类:”之后出现的不是完整的类;这只是SendToCal
方法。请确保问题可以通过提供一个复制。我已经更新了它,以显示整个班级。谢谢请告诉我们你得到的确切输出和你期望的输出。我不明白“python的输出是第一个数组输入的副本”。好吧,我希望这些编辑更清楚。让我知道这是否有意义或者这与PythonInterpreter.initialize()有关吗?根据文档,此方法只能调用一次。谢谢这确实帮助我将参数传递给python脚本!这里的开始、结束和主题是什么?@mannedear:它们是传递给SendtoCal
方法的参数。