0

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?

asked Feb 15, 2012 at 15:17
4
  • please suggest on the above code, if it could be made better/efficient. How shall I check for infinite loop cases? Commented Feb 15, 2012 at 15:18
  • You do realize that allowing user-submitted Java code to be run is very dangerous, right? Commented 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. Commented Feb 15, 2012 at 15:22
  • 2
    Then 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. Commented Feb 15, 2012 at 15:26

1 Answer 1

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.

answered Feb 15, 2012 at 15:23
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. but how do online compilers like ideone.com handle these dangerous situations.
@John That's not an easy question to answer, a lot of stuff has to be taken into consideration. This is something that deserves a question of its own. But mostly I'd assume it's something about creating a sandbox for each application, only allowing a certain amount of memory to be consumed, only allow a certain subset of classes to be used, having applications monitor the user-applications and terminate if they're misbehaving or running longer than they're allowed, etc.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.