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();