Reputation: 11228
I have the below code to run a java program from a file.
import subprocess
ccmd = ['javac', 'T.java']
process = subprocess.Popen(ccmd)
process.wait()
rcmd = ['java', 'T']
output = ""
process = subprocess.Popen(rcmd, stdout=subprocess.PIPE)
output = process.stdout.read()
print output,
But I want the java code to be submitted online, which would be available as POST message. Is there any effective way to run the code without saving it in a .java file and then running the above code?
Upvotes: 0
Views: 1888
Reputation: 19466
No, I don't think there is a way to run the code without saving it to the drive, since the Java compiler only takes files as input and produce files as output.
But you could rather simply save the file in a temporary directory, compile it, run it, and delete the two files (the posted .java file and compiled .class file). You can even delete these files while they're being executed.
But please note that executing arbitrary, user-submitted Java code is very dangerous.
Upvotes: 1