定义-jar文件并从中获取结果 [英] Statring a -jar file and getting results from it
问题描述
大家好!
我想从.net CONSOLE应用程序中启动一个jar文件,并使用以下过程来完成此任务:
/// <summary> /// Executes a process and waits for it to end. /// </summary> /// <param name="cmd"> Full Path of process to execute.</param> /// <param name="cmdParams"> Command Line params of process</param> /// <param name="workingDirectory"> Process' working directory</param> /// <param name="timeout"> Time to wait for process to end</param> /// <param name="stdOutput"> Redirected standard output of process</param> /// <returns> Process exit code</returns> private static int ExecuteProcess(string cmd, string cmdParams, string workingDirectory, int timeout, out string stdOutput) { using (Process process = Process.Start(new ProcessStartInfo(cmd, cmdParams))) { process.StartInfo.WorkingDirectory = workingDirectory; process.StartInfo.UseShellExecute = false ; process.StartInfo.RedirectStandardOutput = true ; process.Start(); stdOutput = process.StandardOutput.ReadToEnd(); process.WaitForExit(timeout); return process.ExitCode; } }
下面的屏幕显示了代码及其输出.我认为这可以回答问题.
下面列出了我在屏幕截图中显示的代码.它用 StreamReader. ReadToEnd()从控制台获取所有字符串.请在上面的命令窗口中查看输出. StreamReader. ReadToEnd()可以读取到最后的所有行.
代码
==== >
使用 使用 使用
class IORedirExample
{
公共 静态 无效 Main()
{
字符串 [] args = 环境 .GetCommandLineArgs();
如果(args.Length> 1)
{
//这是产生的过程的代码
控制台 .WriteLine(重定向过程中的Hello!" );
控制台 .WriteLine(重定向过程中的Hello!" );
控制台 .WriteLine(重定向过程中的Hello!" );
}
其他
{
//这是基本流程的代码
处理 myProcess = 新建 处理();
//启动该程序的新实例,但指定生成的"版本.
ProcessStartInfo myProcessStartInfo = 新建 ProcessStartInfo (args [0], " spawn1" );
myProcessStartInfo.UseShellExecute = false ;
myProcessStartInfo.RedirectStandardOutput = 真;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
//读取生成的过程的标准输出.
字符串 myString = myStreamReader.ReadToEnd();
控制台 .WriteLine(myString);
myProcess.WaitForExit();
myProcess.Close();
}
}
}
Hello everybody!!!
I want to start a jar file from my .net CONSOLE application and I use the following procedure to accomplish this task:
/// <summary> /// Executes a process and waits for it to end. /// </summary> /// <param name="cmd"> Full Path of process to execute.</param> /// <param name="cmdParams"> Command Line params of process</param> /// <param name="workingDirectory"> Process' working directory</param> /// <param name="timeout"> Time to wait for process to end</param> /// <param name="stdOutput"> Redirected standard output of process</param> /// <returns> Process exit code</returns> private static int ExecuteProcess(string cmd, string cmdParams, string workingDirectory, int timeout, out string stdOutput) { using (Process process = Process.Start(new ProcessStartInfo(cmd, cmdParams))) { process.StartInfo.WorkingDirectory = workingDirectory; process.StartInfo.UseShellExecute = false ; process.StartInfo.RedirectStandardOutput = true ; process.Start(); stdOutput = process.StandardOutput.ReadToEnd(); process.WaitForExit(timeout); return process.ExitCode; } }
The screen show below shows the code and its output. I think this answers the questions.
I have listed the code shown at the screen shot, below. It uses StreamReader.ReadToEnd() to get all the strings from the console. Please see the output in the command window above. StreamReader.ReadToEnd() can read all the line to the end.
Code
====
using System;
using System.IO;
using System.Diagnostics;
class IORedirExample
{
public static void Main()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
// This is the code for the spawned process
Console.WriteLine("Hello from the redirected process!");
Console.WriteLine("Hello from the redirected process!");
Console.WriteLine("Hello from the redirected process!");
}
else
{
// This is the code for the base process
Process myProcess = new Process();
// Start a new instance of this program but specify the 'spawned' version.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn1");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadToEnd();
Console.WriteLine(myString);
myProcess.WaitForExit();
myProcess.Close();
}
}
}
这篇关于定义-jar文件并从中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!