使用System.Diagnostics.Process启动Jar文件 [英] Starting a Jar file using System.Diagnostics.Process

查看:121
本文介绍了使用System.Diagnostics.Process启动Jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jar文件,我想在C#中运行。

I have a jar file which I want to run from within C#.

这是我到目前为止所拥有的:

Here's what I have so far:

clientProcess.StartInfo.FileName = @"java -jar C:\Users\Owner\Desktop\myJarFile.jar";
            clientProcess.StartInfo.Arguments = "[Something]";

            clientProcess.Start();
            clientProcess.WaitForExit();

            int exitCode = clientProcess.ExitCode;

不幸的是我得到系统找不到指定的文件,这是有道理的,因为它不是一个文件一个命令。

Unfortunatly I get "System could not find specified file", which makes sense since its not a file its a command.

我见过在线代码告诉你使用:

I've seen code online which tells you to use:

System.Diagnostics.Process.Start("java -jar myprog.jar");

但是我需要返回代码而我需要等待它退出。

However I need the return codes AND I need to wait for it to exit.

谢谢。

推荐答案

终于解决了。文件名必须是java,参数必须包含jar文件的位置(以及你要传递的任何参数)

Finally solved it. The filename has to be java and the arguments has to contain the location of the jar file (and anything arguments you want to pass that)

System.Diagnostics.Process clientProcess = new Process();
clientProcess.StartInfo.FileName = "java";
clientProcess.StartInfo.Arguments = @"-jar "+ jarPath +" " + argumentsFortheJarFile;
clientProcess.Start();
clientProcess.WaitForExit();   
int code = clientProcess.ExitCode != 0)

这篇关于使用System.Diagnostics.Process启动Jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆