1

I have a Java .jar application that is ran from a .bat file to have arguments passed to the Java application. The application opens a console (cmd.exe to be exact), writes things to it regularly and accepts some commands. I'm trying to create a kind-of wrapper around it in C# Winforms to ease it's use. How can I run the .jar with the same arguments as are in the .bat file, capture realtime output and write execute commands?

Spans
  • 364
  • 7
  • 17
  • You can do this using the [System.Diagnostics.Process class](http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process). What have you tried so far? – keenthinker Apr 04 '14 at 19:03
  • @pasty I haven't tried anything so far, only looked up if it could be done. I won't bother starting the project only to find out what I want to is impossible. – Spans Apr 04 '14 at 19:04
  • It is possible. Since it is a fairly common operation, every widely spread modern programming language supports such a feature, [Java for example supports it too](http://stackoverflow.com/questions/3774432/starting-a-process-in-java). – keenthinker Apr 04 '14 at 19:51

1 Answers1

3

Yes, it is possible to do this using the System.Diagnostics.Process class and the ProcessStartInfo class from the .NET Framework. The Process class is used to control (start / stop) the desired process (application) and the ProcessStartInfo class is used to configure the process instance that will be started (arguments, redirect input and output, show / hide process window, and so on).

The code for starting a jar file looks like this:

var jarFile = "D:\\software\\java2html\\java2html.jar");
// location of the java.exe binary used to start the jar file
var javaExecutable = "c:\\Program Files (x86)\\Java\\jre7\\bin\\java.exe";

try
{
    // command line for java.exe in order to start a jar file: java -jar jar_file
    var arguments = String.Format(" -jar {0}", jarFile);
    // create a process instance
    var process = new Process();
    // and instruct it to start java with the given parameters
    var processStartInfo = new ProcessStartInfo(javaExecutable, arguments);
    process.StartInfo = processStartInfo;
    // start the process
    process.Start();
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message);
}

The usual way to start a jar file is:

java -jar file.jar

To be sure, that the process will find the (in this case java) executable, it is a good practice to specify the fully qualified path to the process you want to start.

In order to redirect the standard output of the application you are starting, you need to set the ProcessStartInfo.RedirectStandardOutput property to true and then use the Process.StandardOutput property stream to fetch the output of the started application. The modified code for the application from the example above looks like this:

// command line for java.exe in order to start a jar file: java -jar jar_file
var arguments = String.Format(" -jar {0}", jarFile);
// indicate, that you want to capture the application output
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
// create a process instance
var process = new Process();
// and instruct it to start java with the given parameters
var processStartInfo = new ProcessStartInfo(javaExecutable, arguments);
process.StartInfo = processStartInfo;
// start the process
process.Start();
// read the output from the started appplication
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

If you want to control the input too, set the ProcessStartInfo.RedirectStandarInput property to true and then use the Process.StandardInput property stream to send input data to the started application.

keenthinker
  • 7,645
  • 2
  • 35
  • 45