Question: Connecting Python To Java
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...


// run this way

// javac

// java -classpath . JavaRunCommand

public class JavaRunCommand {

    public static void main(String args[]) {

    String st = null;

    try {

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

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

        BufferedReader stdInput = new BufferedReader(new


        BufferedReader stdError = new BufferedReader(new


        // read the output

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



        // read any errors

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





    catch (IOException e) {

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






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
gravatar for Medhat
6.8 years ago by
Medhat8.5k wrote:

i think you should try this instead,

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

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 after that you can execute python file as ./

ADD REPLYlink modified 6.7 years ago • written 6.7 years ago by samsara600
Please log in to add an answer.


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