Hi Guys,
So in my process of developing a util that utilizes “A well known javascript compressor” to compress javascript files. I came across where I had a need to run Java.exe from my c# application.
You must be aware of Process class under System.Diagnostics package.
We will be utilizing Process class to execute java from command line and keep the window hidden and also stop the flow of program until called EXE finishes execution
If this solution can be improved please leave you comments but I guess its pretty much standard using c# libraries so can’t go wrong with that
This is how I did it finally.
[geshi lang=”csharp” nums=”1″ target=”_self” ]
1 2 3 4 5 6 |
string arg = @" -jar jcgp.jar -user=someone";// just an example this can be anything string command = "java.exe"; ProcessStartInfo start = new ProcessStartInfo(command, arg); start.UseShellExecute = false; start.CreateNoWindow = true; // Important if you want to keep shell window hidden Process.Start(start).WaitForExit(); //important to add WaitForExit() |
Important piece of information in above code is start.CreateNoWindow = true, by default this value is false, When you set it to true, then you are directing your program that keep the called program hidden.
When you call WaitForExit on the process it sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited.
Read more here about WaitForExit
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx
Leave a Reply