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

Recommended Answers

All 2 Replies

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 :)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.