0

In my recent task I need to execute a java command and get some value from it and use it in my program.

I read lot about it in the online forum but non is working for me.

I tried creating bat file of it and executing it in my program but that also is not working.

When I execute it in the command prompt or executing the bat file directly then it works. But when I execute from the application/exe then it fails.

I need the output also.

Any help is highly appreciated.

Thanks in advance.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
user581453
  • 59
  • 1
  • 8
  • Can you post details of the batch file that works – Chelseawillrecover Oct 29 '13 at 06:29
  • The environement variables must be avaialble when you are running it from Command prompt which would not be available while executing it from code.. Can you share whats the error that you are seeing when running it from app? – CHash11 Oct 29 '13 at 06:31
  • "It does not work" is not an error description. We cannot help you this way. Post details. – nvoigt Oct 29 '13 at 06:37
  • This please, as we are rather unable to solve your problem: http://stackoverflow.com/questions/how-to-ask – Tafari Oct 29 '13 at 06:54
  • Hi, Sorry guys. I found the error. My m/c has multiple version of java. And when I was executing from the cmd it was working fine. But when I used to execute from the code/exe it failed because it used different version of java. After some R&D I found the issue and solved it by using the absolute path of java Thanks all for helping me. :) – user581453 Oct 29 '13 at 10:13

2 Answers2

0

The best way would be to use the process class it will allow you to capture standard output and send input to the application.

http://msdn.microsoft.com/en-us/library/System.Diagnostics.Process(v=vs.110).aspx

Rhys Bevilaqua
  • 2,102
  • 17
  • 20
0

Sample code:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

The question may help you :How To: Execute command line in C#, get STD OUT results

Community
  • 1
  • 1
huoxudong125
  • 1,966
  • 2
  • 26
  • 42