简体   繁体   English

从.Net调用Java方法

[英]Call Java method from .Net

I've to call a java method from a C# .Net concole application. 我必须从C#.Net concol应用程序中调用java方法。

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 (eg a string object) from the jar file to the .Net console app. 不允许从jar文件向.Net控制台应用程序返回有用的值(例如,字符串对象)。

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. 另一种方法可以使用IKVM,但是开发已经结束,并且在稳定的企业解决方案中使用它似乎已经很老了。

How can I call a java method and geta string as result value? 如何调用java方法和geta字符串作为结果值?

IKVM is pretty heavyweight (not to mention discontinued) so if you can avoid it then it might be easier. IKVM非常重量级(更不用说已停产),因此,如果可以避免它,它可能会更容易。

If the Java program can produce its output on STDOUT (ie write to the console) then you could read that output via your Process object. 如果Java程序可以在STDOUT上生成其输出(即写入控制台),则可以通过Process对象读取该输出。

For example: 例如:

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

You may need to experiment with setting other properties on your ProcessStartInfo . 您可能需要尝试在ProcessStartInfo上设置其他属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM