I'm writing a multi-language IDE in C#. Right now, it works as a reasonably functional editor. However, I'm working now on adding the ability to compile and run Java code (I know it sounds silly to write this in C#, but I prefer using C#, and I need the Java support for a school class).
After that background, here is the question: What is the best way to compile Java code from C#? Right now, I am using the System.Diagnostics.Process
class. Basically, I am invoking the compiler the same way you would call javac from the command line. Here is a rough example of my current implementation:
ProcessStartInfo buildInfo = new ProcessStartInfo();
buildInfo.WorkingDirectory = Path.GetDirectoryName(sourcePath);
buildInfo.RedirectStandardOutput = true;
buildInfo.RedirectStandardError = true;
buildInfo.UseShellExecute = false;
Process buildProcess = Process.Start(buildInfo);
I redirect standard output and error because later the application catches them to determine whether the compilation was successful. Is there a better way to do this? It feels a little sloppy, and I wanted to know if I was overlooking something.
You do have other options. Java provides JNI (Java Native Interface ) which allows Java to call native code and apropos, for native code to call Java (albeit in a rather complex way.)
Depending on how much of a learning experience you want this to be you can use JNI directly or use a library such as jni4net. A different interop approach is to use ikvm which is a jvm running inside the clr, but I don't think it'll be useful to you as it does not include a compiler.
You can also research alternative compilers such as gcj or ejc.
Not having tried to write an IDE I don't know whether these approaches are actually better than using the command line directly. My hunch is that for simple integration the command line is the easier to use however more complex scenarios, e.g. incremental compilation of large projects with multiple components, may require tighter integration.
If you plan on providing features such as inline debugging and error highlighting while you type you're going to require tighter integration anyway.
IDEs are extremely complex programs, even mere programming editors are complex enough.