我有一个 jar 文件,我想在 C# 中运行它。
这是我目前所拥有的:
clientProcess.StartInfo.FileName = @"java -jar C:\Users\Owner\Desktop\myJarFile.jar";
clientProcess.StartInfo.Arguments = "[Something]";
clientProcess.Start();
clientProcess.WaitForExit();
int exitCode = clientProcess.ExitCode;
不幸的是,我得到“系统找不到指定的文件”,这是有道理的,因为它不是一个文件,而是一个命令。
我在网上看到代码告诉你使用:
System.Diagnostics.Process.Start("java -jar myprog.jar");
但是我需要返回码并且我需要等待它退出。
谢谢。
最佳答案
终于解决了。文件名必须是 java,参数必须包含 jar 文件的位置(以及您要传递给它的任何参数)
System.Diagnostics.Process clientProcess = new Process();
clientProcess.StartInfo.FileName = "java";
clientProcess.StartInfo.Arguments = @"-jar "+ jarPath +" " + argumentsFortheJarFile;
clientProcess.Start();
clientProcess.WaitForExit();
int code = clientProcess.ExitCode;
关于c# - 使用 System.Diagnostics.Process 启动 Jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997915/