Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a Java program from C#?

Tags:

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.

like image 346
7wp Avatar asked May 17 '09 02:05

7wp


People also ask

How can I call a Java program from C program?

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.

How is Java code executed step by step?

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 .

Can we run Java in Dev C++?

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.


2 Answers

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(); 
like image 74
Jase Whatson Avatar answered Sep 17 '22 16:09

Jase Whatson


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.

like image 37
Thorbjørn Ravn Andersen Avatar answered Sep 17 '22 16:09

Thorbjørn Ravn Andersen