Connecting Python To Java
1
0
Entering edit mode
11.9 years ago

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 • 10.0k views
ADD COMMENT
0
Entering edit mode

The precise error messages would help people to answer.

ADD REPLY
0
Entering edit mode

Did you have a look at Jython?

ADD REPLY
1
Entering edit mode
11.9 years ago
Medhat 9.8k

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();
ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2446 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6