index.js
function runInServlet(request, cb) {
    // log("waitingQueue:"+waitingQueue.length+", runningQueue:"+runningQueue.length);
    // log("pushed into running");
    if (request.cb) {
        cb = request.cb;
    }
    // program to run
    var post_data = querystring.stringify({
        'name': request.name,
        'code': request.program,
        'input': request.input,
        'timeLimit': request.timeLimit
    });
    // An object of request to indicate where to post to
    var post_options = {
        host: '127.0.0.1',
        port: server.getPort(),
        path: '',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': post_data.length
        }
    };

    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');

        var responseString = '';

        res.on('data', function(data) {
            // clearTimeout(request.timer);
            // log(data);
            // debugger;
            if (/An exception has occurred in the compiler/.test(data) || /RuntimeError: java.lang.ThreadDeath/.test(data)) {
                // log("ReWrote the folwoing as timout exception "+data);
                // queue.checkQueues();
                request.timeOut();
            } else {
                data = JSON.parse(data);
                request.setToDone();
                observer.emit("runner.finished", request);
                // queue.clearRunning();
                // queue.checkQueues();
                cb(null, data.stout, data.sterr);
            }
            // log("finished with one");
        });

        // res.on('end', function() {
        //     log('::-----end-----::');
        //     var data = JSON.parse(responseString);
        //     log(responseString);
        //     log('::-----end-----::');
        //     cb(null, data.stout, data.sterr);
        // });
    });

    post_req.on('error', function(e) {
        log("Error while waiting for server response ", e);
        request.timeOut();
        // cb(e);
    });
    // request.timer = setTimeout(function () {
    //     cb(null, "","TimeoutException: Your program ran for more than "+timeLimit+"ms");
    // },timeLimit+200);
    post_req.write(post_data);
    post_req.end();
}
function runInServlet(request, cb) {
    // log("waitingQueue:"+waitingQueue.length+", runningQueue:"+runningQueue.length);
    // log("pushed into running");
    if (request.cb) {
        cb = request.cb;
    }
    // program to run
    var post_data = querystring.stringify({
        'name': request.name,
        'code': request.program,
        'input': request.input,
        'timeLimit': request.timeLimit
    });
    // An object of request to indicate where to post to
    var post_options = {
        host: '127.0.0.1',
        port: server.getPort(),
        path: '',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': post_data.length
        }
    };

    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');

        var responseString = '';

        res.on('data', function(data) {
            // clearTimeout(request.timer);
            // log(data);
            // debugger;
            if (/An exception has occurred in the compiler/.test(data) || /RuntimeError: java.lang.ThreadDeath/.test(data)) {
                // log("ReWrote the folwoing as timout exception "+data);
                // queue.checkQueues();
                request.timeOut();
            } else {
                data = JSON.parse(data);
                request.setToDone();
                observer.emit("runner.finished", request);
                // queue.clearRunning();
                // queue.checkQueues();
                cb(null, data.stout, data.sterr);
            }
            // log("finished with one");
        });

        // res.on('end', function() {
        //     log('::-----end-----::');
        //     var data = JSON.parse(responseString);
        //     log(responseString);
        //     log('::-----end-----::');
        //     cb(null, data.stout, data.sterr);
        // });
    });

    post_req.on('error', function(e) {
        log("Error while waiting for server response ", e);
        request.timeOut();
        // cb(e);
    });
    // request.timer = setTimeout(function () {
    //     cb(null, "","TimeoutException: Your program ran for more than "+timeLimit+"ms");
    // },timeLimit+200);
    post_req.write(post_data);
    post_req.end();
}
function runInServlet ( request , cb ) { // log("waitingQueue:"+waitingQueue.length+", runningQueue:"+runningQueue.length); // log("pushed into running"); if ( request . cb ) { cb = request . cb ; } // program to run var post_data = querystring . stringify ( { 'name' : request . name , 'code' : request . program , 'input' : request . input , 'timeLimit' : request . timeLimit } ) ; // An object of request to indicate where to post to var post_options = { host : '127.0.0.1' , port : server . getPort ( ) , path : '' , method : 'POST' , headers : { 'Content-Type' : 'application/x-www-form-urlencoded' , 'Content-Length' : post_data . length } } ; var post_req = http . request ( post_options , function ( res ) { res . setEncoding ( 'utf8' ) ; var responseString = '' ; res . on ( 'data' , function ( data ) { // clearTimeout(request.timer); // log(data); // debugger; if ( / An exception has occurred in the compiler / . test ( data ) || / RuntimeError: java.lang.ThreadDeath / . test ( data ) ) { // log("ReWrote the folwoing as timout exception "+data); // queue.checkQueues(); request . timeOut ( ) ; } else { data = JSON . parse ( data ) ; request . setToDone ( ) ; observer . emit ( "runner.finished" , request ) ; // queue.clearRunning(); // queue.checkQueues(); cb ( null , data . stout , data . sterr ) ; } // log("finished with one"); } ) ; // res.on('end', function() { //     log('::-----end-----::'); //     var data = JSON.parse(responseString); //     log(responseString); //     log('::-----end-----::'); //     cb(null, data.stout, data.sterr); // }); } ) ; post_req . on ( 'error' , function ( e ) { log ( "Error while waiting for server response " , e ) ; request . timeOut ( ) ; // cb(e); } ) ; // request.timer = setTimeout(function () { //     cb(null, "","TimeoutException: Your program ran for more than "+timeLimit+"ms"); // },timeLimit+200); post_req . write ( post_data ) ; post_req . end ( ) ; }
Run java program in server, which is singnificantly faster then CMD
@param  {Object} options an object containing all prgram needed configuration
- {String}   name    name of class
- {String}   program source code of JavaClass with public class [name]
- {String}   input   The program's input stream if needed
@param  {Function} cb      callback when complete
Run java program in server which is singnificantly faster then CMD