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?
-
please suggest on the above code, if it could be made better/efficient. How shall I check for infinite loop cases?John Eipe– John Eipe2012年02月15日 15:18:51 +00:00Commented Feb 15, 2012 at 15:18
-
You do realize that allowing user-submitted Java code to be run is very dangerous, right?kba– kba2012年02月15日 15:19:56 +00:00Commented Feb 15, 2012 at 15:19
-
hm.. not exactly. I'm new at this. I'm trying to create something like ideone.com but only for java.John Eipe– John Eipe2012年02月15日 15:22:14 +00:00Commented Feb 15, 2012 at 15:22
-
2Then I suggest you leave this idea entirely. A lot of security measures have to be put in place. You're basically telling everybody that they can execute whatever program they want on your system.kba– kba2012年02月15日 15:26:51 +00:00Commented Feb 15, 2012 at 15:26
1 Answer 1
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.