How to run a Python script from Java

I had a requirement of running a Python script but I am a java guy. So I wrote a code in Java to invoke the python script. We can use Java’s Process API to execute any executable.

public class FaceappPython {
	public static void main(String[] args) {
String PYTHON_HOME = "PATH WHERE PYTHON IS INSTALLED";
String PYTHON_FILE= "FILE CONTAINING PYTHON CODE";
		Process p = null;
		try {
			p = Runtime
					.getRuntime()
					.exec(PYTHON_HOME, PYTHON_FILE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
	}
}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *