ahouse101
ahouse101

Reputation: 362

What are some ways to compile Java from C# code?

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.

Upvotes: 8

Views: 3733

Answers (4)

Eli Algranti
Eli Algranti

Reputation: 9007

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.

Upvotes: 3

cynic
cynic

Reputation: 5405

You could use the Java Invocation API to host the JVM within your process and then run the compiler through the compiler API. It's probably faster than spinning up separate compiler process for compilation and certainly makes for a good learning experience (you'd need to use P/Invoke from the .NET side etc.).

Upvotes: 2

Tim S.
Tim S.

Reputation: 56536

A quick googling suggests (no surprise) there's no .Net API for compiling Java. Thus, the simplest way will probably be to use javac via the command line, as you are currently doing.

Your command line code looks similar to examples I've seen, so I don't expect there's much room for improvement. Thus, the short answer to your question: no, you're good (as good as compiling Java from C# can be, anyway).

Upvotes: 2

Related Questions