Collabedit Java compiler and runner

Posted by & filed under How I solved my problem, In English, Tech.

I have an interview tonight, where I am told that I am supposed to do Java coding on a shared document via the collabedit.com platform.
As I am checked on both speed and correctness I figured that it would be nice to know when I did obvious syntax errors, but as far as I can see, collabedit supports syntax highlighting but does not support any error detecting or running of the actual program.
But it is possible to download your shared document, with the click of a mouse.
This is why I have just hacked together a small python script that listens for a file to be downloaded, and then runs the javac and java tools on it.
I call the script collabedit-java-compiler.py, and it is created to be used on a linux environment, you might be able to use it on other platforms by fx changing the os.system("clear") to os.system("cls") or likewise.

If you have any suggestions, feel free to comment.

#!/usr/bin/python
# Author: Kraen Hansen (www.creen.dk)
# Date: November 2011
import sys
import os.path
from time import sleep
 
def compile_java(filename, classname, autorun):
	print "Compiling "+filename
	result = os.system("javac "+filename)
	if result == 0:
		print "Compiled successfully!"
		should_run = autorun
		if not should_run:
			run = raw_input("Would you like to run it? [Y/n]")
			if(run != "n" and run != "N"):
				should_run = True
		if should_run:
			os.system("clear")
			folder = os.path.split(filename)[0]
			runcommand = "java -cp ""+folder+"" "+classname
			print "Running the program! ("+runcommand+")"
			print "------------------------------"
			print ""
			result = os.system(runcommand)
			print ""
			print "------------------------------"
			print "Program terminated with the signal:", result
			raw_input("Press any key to continue.")
	else:
		raw_input("Press any key to continue.")
 
def collabedit_java_compiler(filename, classname, autorun):
	running = True
	while(running):
		os.system("clear")
		if(os.path.isfile(filename)):
			print "Found the source file!"
			compile_java(filename, classname, autorun)
			print "Removing the file ..."
			os.remove(filename)
		else:
			print "Didn't find a file ... waiting 1 sec"
		sleep(1)
 
if __name__ == "__main__":
	print "Started the collabedit java compiler."
	if len(sys.argv) == 3:
		filename = sys.argv[1]
		classname = sys.argv[2]
		collabedit_java_compiler(filename, classname, True)
	else:
		print """Usage: "path to downloaded file" "Main java class":"""
		print """Example: ./collabedit-java-compiler.py /home/creen/Downloads/n6qp8.java Test"""

One Response to “Collabedit Java compiler and runner”

  1. anon

    Hey thanks, I have an interview too and was just about to write my own, figured i do a search for one first. Goodluck.

    Reply

Leave a Reply