Wondering if anyone knows a nice way to execute a Java command-line program from C# code at run-time ?
Is it the same as executing native .EXE files ?
Will it run synchronously or asynchronously (which means I may have to wait for the thread to finish to find out the results)
Specifically I would like to call a little utility (which happens to be written in Java) from a web-application on the server side to do some processing on a text file. I want to wait for it to finish because after the Java program is done processing the text file I want to grab the processed text, and use it within the C# application.
To call a specific Java function from C, you need to do the following: Obtain the class reference using the FindClass(,,) method. Obtain the method IDs of the functions of the class that you want to call using the GetStaticMethodID and GetMethodID function calls.
In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension .
Code in Java, Execute as C++. Depending on the problem, one might work better than the other. However, at some point, we need to integrate these languages, e.g. calling a method written in Java to your C++ code. The need to integrate Java and C++ is not something new. In fact, we can find a tutorial dated back to 1996.
var processInfo = new ProcessStartInfo("java.exe", "-jar app.jar") { CreateNoWindow = true, UseShellExecute = false }; Process proc; if ((proc = Process.Start(processInfo)) == null) { throw new InvalidOperationException("??"); } proc.WaitForExit(); int exitCode = proc.ExitCode; proc.Close();
If you need finer control than launching an external program, then consider IKVM - http://www.ikvm.net/ - which provides a way to run Java programs inside a .NET world.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With