1

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 !

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hello ! Thanks for this quick reply ! Actually i'm developping the web app with PHP ( Laravel ), but some guys told me that i can use node.js to execute the compiler, this is why i tagged node.js. Also, yes the compiler is jar file, so it is not that complicated, i'm just asking for what technology is best suited to do this kind of work ?
No problem! Does what I posted answer your question?
Hello ! Thanks for this quick reply ! Actually i'm developping the web app with PHP ( Laravel ), but some guys told me that i can use node.js to execute the compiler, this is why i tagged node.js. Also, yes the compiler is jar file, so it is not that complicated, i'm just asking for what technology is best suited to do this kind of work ?
Ah, well then with PHP it's even simpler. I wouldn't use node.js just to run the command. Now if you need it for something else that's fine, but PHP lets you execute external commands very easily. I have edited my answer to add how to do it with PHP.
Thank you very much !! I really appreciate !
|
1

A typical way of doing this is to create a REST API with a controller that handles POST requests. In the data of the request you need to send the input source code, and the data of the response should contain the compiled code.

If the compilation is encapsulated in a function called "compile", your REST controller would look something like this, using the Spring framework:

@RestController
public class HelloController {

    @RequestMapping(value = "/compile", method = RequestMethod.POST)
    public String compilationAPI(@RequestBody SourceCode sourceCode) {
        String compiledCode = compile(sourceCode);
        return compiledCode;
    }

}

3 Comments

Thank you for the reply ! But as i said the compiler is done, and i don't think that the guys behind it would like to change the code...They have a working jar..so...
I thought you wanted help with understanding how to create the interaction between the client and the server. Do you need help with invoking the compiler programmatically, and loading the output file?
This is what i want to do. In the web app, you click on compile, we launch the compiler programmatically and the download the compiled file !
-1

try this:
https://tomcat.apache.org/download-70.cgi

some tutorial:
https://www.youtube.com/watch?v=3CXBRjpiTws
http://www.coreservlets.com/Apache-Tomcat-Tutorial/
http://www.tutorialspoint.com/servlets/

you can use eclipse to start a tomcat server to test your code.

1 Comment

Thank you for the quick reply ! I don't think we can switch to tomcat juste for as such a simple task. Thank you anyway !

Your Answer

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.