3

I wanted to run a script from java class. I was able to do it with ProcessBuilder. Now I am struggling to pass data as an argument to the script. I can pass a string, but I am having trouble passing list of data (data read from a CSV file). One that I tried was using StringBuffer and passing the data as Bytes

    StringBuffer sb = new StringBuffer();
    ArrayList argsList = new ArrayList();

    // append the sb from record. 
    //convert it to bytes 
    //sb.toString() contains my data with a delimiter ';'

    // sb.toString().getBytes("UTF-8").toString()

    argsList.add(0,"Continuum/anaconda3/envs/sth/python");
    argsList.add(1,"python/test.py");
    argsList.add(2,"python/test.py");
    argsList.add(3,sb.toString().getBytes("UTF-8").toString())

    ProcessBuilder builder = new ProcessBuilder(argsList);

    Process p = builder.start();

I can not pass sb.toString().getBytes("UTF-8") to the process builder. I get java.lang.ArrayStoreException

On the other hand, if I pass sb.toString(), I get

java.io.IOException: Cannot run program Continuum/anaconda3/envs/sth/python, CreateProcess error=206, The filename or extension is too long
add it to list of commands to process builder. 

How can I read what I passed in python?

Is this even the right way to pass data ( row X column)?

Thanks

nomadSK25
  • 2,350
  • 3
  • 25
  • 36
  • 1
    check this, it may be helpful since it does the same thing using a different approach , https://norwied.wordpress.com/2012/07/23/pass-arguments-from-java-to-python-app/ – Bara' Hashesh Aug 22 '18 at 15:06
  • 1
    did u check this https://stackoverflow.com/questions/27267391/running-a-py-file-from-java/27267509 – pvpkiran Aug 22 '18 at 15:10
  • How long exactly is the string you are trying to pass? There's usually an OS-imposed limit on command line argument length on the order of maybe 100 kilobytes. Also, did you try making your `ArrayList` an `ArrayList` to maybe fix whatever type confusion is giving you an `ArrayStoreException`? – interfect Aug 22 '18 at 18:40
  • How big is your `sb.toString()`? What does your test.py do? – Dakshinamurthy Karra Aug 23 '18 at 07:44

2 Answers2

0

You can try below code ::

import java.io.*;

class RunPythonCommand {
    public static void main(String [] args) {
        try {

            String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
            BufferedWriter out = new BufferedWriter(new FileWriter("sample_code.py"));
            out.write(prg);
            out.close();
            int param1 = 10;
            int param2 = 32;

            ProcessBuilder pb = new ProcessBuilder("python", "sample_code.py", "" + param2, "" + param1);
            Process p = pb.start();

            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            int ret = new Integer(in.readLine()).intValue();
            System.out.println("value is : " + ret);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Hope it will help you

  • param2 is an argument which is passed in sample_code.py . sample_code.py have a code of sum/multiplication code. – Anil Tiwari Nov 27 '18 at 07:04
0

You can pass args as array.:

final String PYTHON = "python";

final String SCRIPT_PATH = new File("Pyscript.py").getPath();

final String ACTION = "-aexport";

final String[] CMD_ARRAY = {PYTHON, SCRIPT_PATH, ACTION};

ProcessBuilder processBuilder = new ProcessBuilder(CMD_ARRAY);

Palla
  • 1,131
  • 9
  • 11