简体   繁体   English

使用System.Diagnostics.Process启动Jar文件

[英]Starting a Jar file using System.Diagnostics.Process

I have a jar file which I want to run from within C#. 我有一个jar文件,我想在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. 但是我需要返回代码,我需要等待它退出。

Thanks. 谢谢。

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) 文件名必须是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 != 0)

You need to set environment variable Path of java.exe executable or specify the full path of java.exe . 你需要设置环境变量Pathjava.exe可执行文件或指定的完整路径java.exe

 ProcessStartInfo ps = new ProcessStartInfo(@"c:\Program Files\java\jdk1.7.0\bin\java.exe",@"-jar C:\Users\Owner\Desktop\myJarFile.jar");
 Process.Start(ps);

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

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