Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Jar file from C sharp code and get return value

Tags:

java

c#

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.

like image 438
user2003167 Avatar asked Jan 23 '13 09:01

user2003167


2 Answers

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#.

like image 145
Ivaylo Strandjev Avatar answered Oct 08 '22 18:10

Ivaylo Strandjev


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();
like image 40
Igor Mascarenhas Avatar answered Oct 08 '22 17:10

Igor Mascarenhas