java 从C尖代码执行Jar文件并获取返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14475944/
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
Execute Jar file from C sharp code and get return value
提问by user2003167
Just I want to execute the Jar file from C sharp code and get return values from jar. Is it possible?
只是我想从 C 锐代码执行 Jar 文件并从 jar 获取返回值。是否可以?
If so give me the sample code.
如果是这样,请给我示例代码。
I tried following thing,
我尝试了以下事情,
string path = "C:\Documents and Settings\Desktop";
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "C:\Program Files\Java\jre6\bin\java.exe";
process.StartInfo.Arguments = "-jar \"" + path + "\simple.jar\"";
process.Start();
String s = process.StandardOutput.ReadToEnd();
here simple.jar has main method which will take the arguements and prints the passed arguemnets in console, otherwise it prints no arguements in console. I tried above code in this line(String s = process.StandardOutput.ReadToEnd();) able to read the console values.
这里 simple.jar 有 main 方法,它将获取参数并在控制台中打印传递的参数,否则它不会在控制台中打印参数。我在这一行中尝试了上面的代码(String s = process.StandardOutput.ReadToEnd();) 能够读取控制台值。
But I want to execute a method by passing values in jar and method will return me hashmap (collection) values(I don't know it is possible or not). Please give me suggestions on this.
但是我想通过在 jar 中传递值来执行一个方法,方法将返回我的 hashmap(集合)值(我不知道这是否可能)。请给我这方面的建议。
回答by Ivaylo Strandjev
回答by Igor Mascarenhas
This works for me
这对我有用
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "java";
myProcess.StartInfo.Arguments = "-jar C:\FileLocal\file.jar";
myProcess.Start();