This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Connecting Python To Java

I have been trying to access the modeller program written in python programming language with my Java code. But I am unable to do so. I have tried accessing the .py file , the .exe file, using jython but still no success. The jython is helping access certain classes not the entire program.

Plz help.

I have tried this code but its giving me some stupid errors which i cant fix.

Plz help...

import java.io.*;



// run this way

// javac JavaRunCommand.java

// java -classpath . JavaRunCommand



public class JavaRunCommand {



    public static void main(String args[]) {



    String st = null;



    try {



        String[]callAndArgs= {\"python\",\"my_python.py\",\"arg1\",\"arg2\"};

        Process p = Runtime.getRuntime().exec(callAndArgs);



        BufferedReader stdInput = new BufferedReader(new

             InputStreamReader(p.getInputStream()));



        BufferedReader stdError = new BufferedReader(new

             InputStreamReader(p.getErrorStream()));



        // read the output

        while ((s = stdInput.readLine()) != null) {

            System.out.println(s);

        }



        // read any errors

        while ((s = stdError.readLine()) != null) {

            System.out.println(s);

        }



        System.exit(0);

    }

    catch (IOException e) {

        System.out.println(\"exception occured\");

        e.printStackTrace();

        System.exit(-1);

    }

    }

}

Thankx in Advance... :)

java python modeling

The precise error messages would help people to answer.

Did you have a look at Jython?

1 answer

i think you should try this instead,

  Runtime.getRuntime().exec("cmd /c start my_python.py");

python files alone are not executable. They need an application to tun them, in this case, cmd.

or in your case i think you should do this

 Process p = Runtime.getRuntime().exec(callAndArgs);  p.waitFor();

Python scripts are executable if you put #! /usr/bin/env python on top of the script and by executing command $ chmod +x myscript.py after that you can execute python file as ./myscript.py

Log in to answer this question.