5

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.

user2003167
  • 121
  • 1
  • 2
  • 5

2 Answers2

7

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

Community
  • 1
  • 1
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

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();