如何从 C# 执行 Java 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/873809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a Java program from C#?
提问by 7wp
Wondering if anyone knows a nice way to execute a Java command-line program from C# code at run-time ?
想知道是否有人知道在运行时从 C# 代码执行 Java 命令行程序的好方法吗?
Is it the same as executing native .EXE files ?
它与执行本机 .EXE 文件相同吗?
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.
具体来说,我想从服务器端的 Web 应用程序调用一个小实用程序(恰好是用 Java 编写的)来对文本文件进行一些处理。我想等待它完成,因为在 Java 程序处理完文本文件后,我想获取处理后的文本,并在 C# 应用程序中使用它。
采纳答案by Thorbj?rn Ravn Andersen
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.
如果您需要比启动外部程序更精细的控制,请考虑 IKVM - http://www.ikvm.net/- 它提供了一种在 .NET 世界中运行 Java 程序的方法。
回答by Pablo Santa Cruz
It's the same as executing native .EXE files, only that the executable you will have to execute is the JVM itself (java.exe).
它与执行本机 .EXE 文件相同,只是您必须执行的可执行文件是 JVM 本身 (java.exe)。
So, inside your C# code call:
因此,在您的 C# 代码调用中:
java.exe -jar nameofyourjavaprogram.jar
java.exe -jar nameof yourjavaprogram.jar
And you should be fine.
你应该没事。
If you don't have your java program on a JAR library, just make the JVM launch with all the parameters you need.
如果您的 JAR 库中没有您的 Java 程序,只需使用您需要的所有参数启动 JVM。
回答by Jase Whatson
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();
回答by Peter Lawrey
Will it run synchronously or asynchronously
是同步运行还是异步运行
It will run asynchronously if you have enough cores, otherwise it run independently, but your thread will have to context switch so the other program will run. Either way its not something you should need to worry about.
如果您有足够的内核,它将异步运行,否则它将独立运行,但您的线程必须进行上下文切换,以便其他程序运行。无论哪种方式,它都不是您应该担心的事情。
回答by Pavel Savara
Maybe it would run faster if you use jni4net - C#/Java bridge
如果您使用jni4net - C#/Java 桥接器,也许它会运行得更快
回答by Chan Souvannarath
I added a couple of lines to the above solution. I wanted to call a Web Service from a Silverlight app that process some files using java on the server. The above solution is helpful but I modified a little bit so that it works since calling via a web service is a little bit trickier. Now you have the right tool for the job, C# when appropriate, Java when C# cannot solve the problem. It's always good to know more than just one way of doing things. Now my Web Service created in .Net can talk to Java.
我在上面的解决方案中添加了几行。我想从 Silverlight 应用程序调用 Web 服务,该应用程序在服务器上使用 java 处理一些文件。上面的解决方案很有帮助,但我做了一些修改,以便它可以工作,因为通过 Web 服务调用有点棘手。现在您有了适合这项工作的工具,C# 合适,Java 则 C# 无法解决问题。知道不止一种做事方式总是好的。现在我在 .Net 中创建的 Web 服务可以与 Java 通信。
private void Merge(string strPath)
{
var processInfo = new ProcessStartInfo("C:\Program Files\Java\jdk1.6.0_24\binjava.exe", "-jar app.jar")
{
CreateNoWindow = true,
UseShellExecute = false
};
processInfo.WorkingDirectory = strPath; // this is where your jar file is.
Process proc;
if ((proc = Process.Start(processInfo)) == null)
{
throw new InvalidOperationException("??");
}
proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
}
回答by Broken_Window
Just for the completeness: When lauching a Swing jar from C# I found this detail: if you don't set the working directory in the ProcessStartInfo object, your shiny Swing app will launch... but with no icons and no images!!
只是为了完整性:当从 C# 启动 Swing jar 时,我发现了这个细节:如果你没有在 ProcessStartInfo 对象中设置工作目录,你闪亮的 Swing 应用程序将启动......但没有图标和图像!
This is the minimal working code copied from the answers here and elsewhere on SO (works for me: Java 1.8 on Win7, mi images and icons are in a subfolder of the workingDirectory):
这是从 SO 上和其他地方的答案复制的最小工作代码(对我有用:Win7 上的 Java 1.8,mi 图像和图标位于 workingDirectory 的子文件夹中):
ProcessStartInfo psi = new ProcessStartInfo("java.exe", " -jar \"C:\Program Files\Installed Shiny Swing jar app\Myjar.jar\"");
psi.WorkingDirectory = "C:\Program Files\Installed Shiny Swing jar app\"; // Do not miss this line so you awesome Swing app will show default java icon and no images
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();