How to call python script from a JAVA code

Hi All,

I have been getting requests for an end to end solution to the problem – “How to run a python script, or any exe from JAVA code”. Google doesn’t have much stuff on this. Most sites are just giving one line of code to call a python script from a java code as shown below.

Process p = Runtime.getRuntime().exec("c:\\tobo\\test.exe");

Now here is the catch, It is true that in a single line of java code you can fire the python script or any other utility, but java code doesn’t wait for the python script’s/utility’s output.

Let’s assume an example.

There are 3 tasks. T1, T2 and T3. Now T1 and T3 are required to be performed by the Java code while task T2 is to be performed by Python utility. And all tasks are to be executed sequentially. First Task T1, then T2 and finally T3.

Now below is my code for the above problem statement.

package in.gotobo.admin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Timestamp;

public class javaToPython {

	public static void main(String[] args) throws IOException {
		try
		{
                       //Here perform Task T1

                       //Task T2 will be performed by below script
			String command = "c:\\tobo\\test.exe";
			Process p = Runtime.getRuntime().exec(command);
                       //Here perform Task T3
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception "+e);
		}
	}
	
}

Now the above code is simple, easy to plug in and execute the python script. But let’s ask the question, Did this code solve our problem statement, which was to execute 3 tasks in sequential manner. Above code will perform T1 and then T2 and T3 in parallel manner not sequentially, because as soon as the java code is calling the ‘test.exe’ it does not wait for the utility’s output, it moves ahead to perform task T3.

Below is the efficient code.

package in.gotobo.admin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Timestamp;

public class javaToPython {

	public static void main(String[] args) throws IOException {
		try
		{		
                       //Here perform Task T1

                       //Task T2 will be performed by below script		
			String command = "c:\\tobo\\test.exe";
			Process p = Runtime.getRuntime().exec(command);
			
			BufferedReader stdInput = new BufferedReader(new 
					InputStreamReader(p.getInputStream()));

			BufferedReader stdError = new BufferedReader(new 
					InputStreamReader(p.getErrorStream()));

			// read the output from the command, Now this part of the code is important
			String s = "";
			while ((s = stdInput.readLine()) != null) {
				System.out.println(s);
			}

			// read any errors from the attempted command
			while ((s = stdError.readLine()) != null) {
				System.out.println(s);
			}
                       //Here perform Task T1
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception "+e);
		}
	}
	
}

In the above code following points are important to note

  • The tasks T1, T2 and T3 are performed one after the other i.e. sequentially.
  • The output of the python script will be printed on the console by JAVA code.
  • The error message if any occurs will also be displayed.

Above is the most complete code to run a python script or any other exe using JAVA code.

Happy Coding.

🙂

4 thoughts on “How to call python script from a JAVA code

  1. May I simply just say what a comfort to uncover somebody that
    actually understands what they are talking about on the net.
    You definitely realize how to bring an issue to light
    and make it important. More people really need to read this and understand this side of the story.

    I was surprised that you are not more popular given that you most certainly have the gift.

  2. You could certainly see your skills within the article you
    write. The world hopes for even more passionate writers like you who aren’t afraid to mention how
    they believe. All the time follow your heart.

  3. I got this site from my friend who told me about this website and
    now this time I am browsing this website and reading very informative content at this time.

  4. It is the best time to make a few plans for the long run and
    it’s time to be happy. I’ve learn this put up and if
    I may just I want to counsel you some interesting things or tips.
    Maybe you can write next articles relating to this article.
    I want to learn even more issues about it!

Leave a Reply to j.mp Cancel reply

Your email address will not be published.

Verified by MonsterInsights