Just I want to execute the Jar file from C sharp code and get return values from jar. Is it possible?
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.
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.
To execute a jar file in the command line use java.exe -jar <jar_name>
.
Take a look here to see how do you execute a command line program and get its output in C#
.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With