Executing Java Code or C++ Code from Python
So, First of all let me clear all the myths that we will execute C++ or Java code from python, it is pretty simple we will execute a subprocess from our python script.
I will use subprocess module for executing subprocess from python script.
The syntax is that we need to pass a list to run method of subprocess module.
For example i need to execute Notepad application so first we need to specify the application (exe file) path.
subprocessExample.pyimport subprocess
application=”C:\\windows\\notepad.exe”
print(“About to start application”)
subprocess.run([application])
print(“Processing Completed”)
When i executed the subprocess, you can see that Processing Completed statement is not printed on the console, that means the python script is in wait mode till the subprocess is executing.
Whenever we will close the notepad application, Processing Completed statement will get printed on the console.
Now if i want that i want to make a file named myExample.cpp using subprocess method then what to do??
When we hard code we type command on command promt
→ notepad myExample.cpp and then notepad application get executed and we asked that cannot find ‘myExample.cpp’ , Do you want to create new file?
So why does this happen?
Reason behind this is we pass myExample.cpp string as command line arguments to Notepad.exe application. This command line argument is treated as the name of the file to be opened if found in directory or to be created as new file. So same we should do, we can pass the arguments to run method of subprocess module in list form. Below is the demonstration.
subprocessExample2.py
import subprocess
application=”C:\\windows\\notepad.exe”
print(“About to start application”)
subprocess.run([application,”eg1.c”])
print(“Processing Completed”)
So you can clearly see the output as we discussed. I gave myExample.cpp as command line agruments in list form to run method and the same thing happend which notepad myExample.cpp does. When i will click yes a file named myExample.cpp will be created in the respective directory. And after closing the notepad application Processing Completed statement will get printed on the console.
Compiling C++ and Java Code from Python Script
eg1.cpp
#include<iostream>
using namespace std;
int main()
{
cout<<”Hey i am C++ code”<<endl;
return 0;
}
subprocessExample3.py
import subprocess
application=”C:\\MinGW\\bin\\g++.exe”
print(“Compiling C++ code”)
subprocess.run([application,”eg1.cpp”,”-o”,”eg1.exe”])
print(“Executing the Compiled Code”)
application=”C:\\work\\python\\codes\\subprocess\\eg1"
subprocess.run([application])
print(“Execution Completed”)
Explanation-:
So here first of all i executed a subprocess named g++ which is GNU compiler for C++ and i passed command line arguments to g++ as
eg1.cpp -o eg1.exe
and called run method of subprocess module which perform the same act as this command in command prompt will do
g++ eg1.cpp -o eg1.exe
This command in command prompt will compile file eg1.cpp and will create a executable file named as eg1.exe
and then i executed the compiled file/application named eg1
So we can clearly see that first Compiling C++ code statement of python script got printed on console and then subprocess’s run method get called and g++ compiled the file named eg1.cpp and then Executing the compiled code statement of python script got printed on console and then while executing the eg1.exe file Hey i am C++ code statement of C++ file got printed which is the proof that C++ got compiled and executable file got executed as a subprocess from a python script.
In similiar way we can compile and execute Java code, let me demonstrate it to you.
eg1.java
class eg1psp
{
public static void main(String gg[])
{
int a=10;
int b=20;
int c=a+b;
System.out.printf(“Sum of %d and %d is %d”,a,b,c);
}
}
subprocessExample4.py
import subprocess
application=”C:\\Program Files\\Java\\jdk-15\\bin\\javac.exe”
print(“Compiling Java code”)
subprocess.run([application,”eg1.java”])
print(“Executing the Compiled Code”)
application=”C:\\Program Files\\Java\\jdk-15\\bin\\java.exe”
subprocess.run([application,”eg1psp”])
print(“Execution Completed”)
Explanation-:
So here first of all i executed a subprocess named javac which is compiler for java and i passed command line arguments to javac as
eg1.java
and called run method of subprocess module which perform the same act as this command in command prompt will do
javac eg1.java
This command in command prompt will compile file eg1.java and will create a class file named as eg1psp.class
and then i executed the class file named eg1psp
java eg1psp (Using this command)
So we can clearly see that first Compiling Java code statement of python script got printed on console and then subprocess’s run method get called and javac compiled the file named eg1.java and then Executing the compiled code statement of python script got printed on console and then while interpreting the eg1psp.class file Sum of 10 and 20 is 30 statement of Java file got printed which is the proof that Java got compiled and class file got interpreted by java as a subprocess from a python script.