Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

How to execute a Java program from C#?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...