2

I'm trying to compile a Python code using Java. The Python code opens a csv file and saves the data to an other csv file.

    import os
    from stat import*
    import csv

    data = []
    with open('Example.csv') as f:
        reader = csv.reader(f, delimiter = ',')
        for row in reader:
            data.append(row)

    f = open("Clearprint.csv","w")
    f.truncate()
    f.close()

    with open('Clearprint.csv','w',newline='') as fp:
        a = csv.writer(fp,delimiter = ',')
        a.writerows(data)

The Python code works as it should but when I try to compile it through Java nothing happens.

    class test1{

        public static void main(String[] args) throws IOException {
            String pythonScriptPath = "C:/Masters/Python/helloPython.py";
            String[] cmd = new String[2];
            cmd[0] = "C:/Python33/python.exe";
            cmd[1] = pythonScriptPath;

            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec(cmd);

            BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line = "";
            while((line = bfr.readLine()) != null) {
                System.out.println(line);
            }
        }   
    }

In the case of the Python code only having a print command everything works fine so it might be something to do with me using a csv. Note that I don't need anything to be returned to Java from Python or be sent to Python. I just want Java to start the Python code and let Python do everything else.

Thank You

3
  • "I'm trying to compile a Python code using Java" - why? Both programs are pretty trivial, why not stick to one language or the other? Alternatively, have a look at Jython.
    – jonrsharpe
    Commented Mar 19, 2015 at 11:28
  • I'm writing a simulation in Anylogic which uses Java. My Anylogic code imports a csv file consisting of a bunch of jobs. I have already coded the manipulation of these jobs in Python and would like to rather just run the Python code from Anylogic, through Java, rather than rewriting all the Python code in Java.
    – Pierre
    Commented Mar 19, 2015 at 11:57
  • Then why not just use Jython to compile your script to Java - see e.g. jython.org/archive/21/docs/jythonc.html
    – jonrsharpe
    Commented Mar 19, 2015 at 12:06

1 Answer 1

1

Your terminology is slightly wrong here, which confuses the issue. You are trying to run a python script by calling the python interpreter from java.

Presumably the python subprocess is throwing some error (like it can't find the file) and the java process is not telling you what that error is. The solution here is to actually capture the errors in java, and print them out:

 BufferedReader err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
    String line = "";
    while((line = err.readLine()) != null) {
        System.err.println(line);
    }

That said, I would agree that doing this in jython might be better - or just re-writing the python part in java - I know, IO and string handling in java is a pain, but not as much as multi-language projects can be :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.