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

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.


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).


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.).

Tags:

C#

Javac