1

I am passing string values to a python script using Jython. In my program, I do this several times. However, during a test I am running to see if the class that sends the parameters is working properly, I see that the python script outputs the same string values as the initial inputs.

Here is the Java class:

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);


 }
}

Also, below is my python script:

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])

My issue is when I call the Java constructor with two or more completely different string arrays, the output from python is a duplicate of the first array input. Any help is appreciated.

I get the following output:

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!!

I expect:

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!!
mzjn
  • 48,958
  • 13
  • 128
  • 248
jvalderr
  • 23
  • 5
  • 1
    The code is incomplete. There are no import statements, and what comes after "Here is the Java class:" is not the full class; it is just the `SendToCal` method. Please ensure that the problem can be reproduced by providing a [mcve]. – mzjn Sep 24 '18 at 05:49
  • I have updated it to show the entire class. Thanks! – jvalderr Sep 25 '18 at 14:39
  • Please show us the exact output that you get and the output that you expect. I don't understand "the output from python is a duplicate of the first array input". – mzjn Sep 25 '18 at 15:10
  • Alright, I hope these edits are more clear. Let me know if this makes sense – jvalderr Sep 25 '18 at 15:32
  • Perhaps this has something to do with `PythonInterpreter.initialize()`? According to the documentation, this method should only be called once. http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html#initialize(java.util.Properties,%20java.util.Properties,%20java.lang.String[]) – mzjn Sep 25 '18 at 16:04
  • I’m not sure how else to pass updated values to the arguments input with each call to this class. Any help? – jvalderr Sep 29 '18 at 03:07

1 Answers1

2

The PythonInterpreter.initialize() method should only be called once. It is called twice in your program and the second time, sys.argv is not updated.

A way around this is to use pyscript.py as a module and call the function defined in the module.

Replace interp.execfile("pyscript.py"); with something like this:

interp.exec("from pyscript import pyscript");
PyObject func = interp.get("pyscript");
func.__call__(new PyString(start), new PyString(end), new PyString(subject));
mzjn
  • 48,958
  • 13
  • 128
  • 248