Search code examples
javanode.jscompilationserverexecute

Execute java code on a server


I'm developping a Web app, a sort of an online IDE to write and compile code. The programming language is developped internally at the university and also the compiler.

My question is : is it possible to execute a compiler on a server ( the compiler is written in java ), so that it compiles the code and returns a compiled file to be downloaded ?

In a simpler fashion, the user uses the online code editor, then clicks on the compile button, the server takes the written code, executes the compiler which is on ther server ( the compiler is written in java ) and then returns the compiled file.

So how could i execute the compiler ( written in java ) on the server ?

Thank you in advance !


Solution

  • You didn't say what type of server, or what language you are using to develop the web app (PHP, node.js, python, perl, etc.), but normally Java distributions have a commandline binary that will run Java code.

    If the compiler file is in a jar file, your command that the webapp executes could be a simple as something like:

    java -jar compiler.jar inputcodefile outputexecutablefile

    Of course you would substitute filenames and add the proper options needed for the compiler (if any).

    EDIT: I see you tagged your question with node.js, so I'm assuming that is the language you are using on the server side.

    node.js has "child process" that allows you to execute external commands. So with the example command I gave above, you would do something like:

    var exec = require('child_process').exec;
    var compileit = 'java -jar compiler.jar inputcodefile outputexecutablefile';
    
    exec(compileit, function(error, stdout, stderr) {
    });
    

    With PHP it's even easier:

    exec('java -jar compiler.jar inputcodefile outputexecutablefile');
    

    See http://php.net/manual/en/function.exec.php for more info on the exec() function in PHP.