3

I've to call a java method from a C# .Net concole application.

The solution at the following link

Process myProcess = new Process();
Process.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "java";
myProcess.StartInfo.Arguments = "-jar D:\\myjava.jar";
myProcess.Start();e

doesn't allow an usefull return value (e.g. a string object) from the jar file to the .Net console app.

Another approach could be use IKVM but the developments are over and it seems to be to old to be used in an stable enterprise solution.

How can I call a java method and geta string as result value?

1 Answer 1

3

IKVM is pretty heavyweight (not to mention discontinued) so if you can avoid it then it might be easier.

If the Java program can produce its output on STDOUT (i.e. write to the console) then you could read that output via your Process object.

For example:

Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "java";
myProcess.StartInfo.Arguments = "-jar D:\\myjava.jar";
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start();
var output = myProcess.StandardOutput.ReadToEnd();

You may need to experiment with setting other properties on your ProcessStartInfo.

Sign up to request clarification or add additional context in comments.

1 Comment

IKVM is not discontinued. We (Windward Studios) have taken over support for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.