Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

javac

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.

like image 684
ahouse101 Avatar asked Jan 28 '13 18:01

ahouse101


People also ask

Is Java compiled in C?

The Java compiler is written as a Java program and then compiled with the Java compiler written in C(the first Java compiler).

Can we use C in Java?

It is a platform dependent intermediate machine code which will be converted to exe and then executed. Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa. Using JNI a java program has the capability to call the native C code.

What is used to compile Java?

The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.

How do I compile and run Java?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.


1 Answers

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.

like image 174
Eli Algranti Avatar answered Oct 15 '22 00:10

Eli Algranti