Question: Connecting Python To Java
0
gravatar for jangamsahil
6.8 years ago by
jangamsahil0 wrote:

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... :)

python java modeling • 5.9k views
ADD COMMENTlink modified 6.8 years ago by Neilfws48k • written 6.8 years ago by jangamsahil0

The precise error messages would help people to answer.

ADD REPLYlink written 6.8 years ago by Neilfws48k

Did you have a look at Jython?

ADD REPLYlink written 6.8 years ago by Pappu1.9k
1
gravatar for Medhat
6.8 years ago by
Medhat8.5k
Texas
Medhat8.5k wrote:

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 COMMENTlink modified 6.8 years ago • written 6.8 years ago by Medhat8.5k

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 REPLYlink modified 6.7 years ago • written 6.7 years ago by samsara600
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1959 users visited in the last hour