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 !
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.
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;
}
}
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.