Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

c# - Execute Jar file from C sharp code and get return value

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...