Question

I have a situation where the user posts a file to server and it needs to be compiled on the server side. The server should detect the language and invoke the corresponding compiler and send the results back to client. Right now we are supporting Java, Python and C. Is there a way I can compile a Java file which is not imported into eclipse from within eclipse? And also, is there a way I can invoke compilers for python and C (or any other language can be used) from within eclipse?

Was it helpful?

Solution

When you detect the language of the code this becomes pretty easy:

String compiler = "";

switch(compilerCode)
{
    case JAVA:
        compiler = "javac";
    break;
    case C:
        compiler = "javac";
    break;

    ...
 }


 Process p = Runtime.getRuntime().exec(compiler + " " + file);
 p.waitFor();

file will be the name of the file you are trying to compile, and compilerCode will be a code that will tell you which compiler to use.

This goes without saying but you should have all of the compilers installed to compile all of the languages you intend on supporting.


Edit:

It is super easy to get error messages from a running process. All you need to do is:

InputStream in = p.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
...
String error = reader.readLine();
...
reader.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top