java 使用 System.Diagnostics.Process 启动 Jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10997915/
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
Starting a Jar file using System.Diagnostics.Process
提问by Aabela
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.
谢谢。
回答by Aabela
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;
回答by adatapost
You need to set environment variable Path
of java.exe
executable or specify the full path of java.exe
.
你需要设置环境变量Path
的java.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);