954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c# process to get return from java jar

I have a batch file that sets JAVA_HOME, PATH and CLASSPATH and calls a jar file. The last line in the batch file is "java -classpath test.jar test %1". When I run the batch file from the command line, using a paramater, the parameter displays in the console as expected.

Now, I need to capture the result in a c# process. In the code below I expect to see the word "testing" in process.StandardOutput, but I only see the contents of the batch file. Can anyone help? Thanks

string batch_file = @"C:\java\runit.bat";
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = batch_file;
process.StartInfo.Arguments = "testing";
process.Start();
process.WaitForExit();
if (process.HasExited)
MessageBox.Show(process.StandardOutput.ReadToEnd());

C#mark
Newbie Poster
2 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

I am incline to think that the only thing being written to the console then is the bat file.

Also looking at your code you haven't told the process where to output to, I figured it might have to go to some sort of stream you can read from. Googling this confirmed my suspicions and I found this handy tid bit http://www.c-sharpcorner.com/UploadFile/edwinlima/SystemDiagnosticProcess12052005035444AM/SystemDiagnosticProcess.aspx

Have a look at it and take note how the output stream is attached to a stream reader.

Hope this helps :)

RabidDog5150
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 5
 

ok I got it to work.
I removed process.StartInfo.Arguments = "testing"; from the c# code and process.StandardOutput.ReadToEnd() now returns the expected result

I had to change the batch file as follows:
@echo off
set JAVA_HOME=C:\...
set PATH=C:\...\bin
set CLASSPATH=C:\path\test.jar;C:\path\lib\ojdbc14.jar
java package.test arg1 arg2
@echo on

C#mark
Newbie Poster
2 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: