How to call an Executable Jar file from Dot Net Windows application?


Many times it is necessary to run an executable jar file from a desktop application. Though executable jar file itself is runnable, a non technical user may find it difficult and weird to launch it from command prompt. In order to provide a user friendly interface, it is good to make a windows forms application in dot net and call the executable jar file at the background. System.Diagnostics.Process class from dot net framework can be used to achieve this.

System.Diagnostics.Process class enables you to start new processes, invoke executable files from your dot net application. The Process class also has methods to redirect the standard output and read it in your windows application. The below code snippet can be used to call an executable jar file from dotnet windows application.

Visual C# Code

using System.Diagnostics;

public void ExecuteCommand(string parameter)
    {
        int exitcode;
        ProcessStartInfo ProcessInfo;
        Process process;
        ProcessInfo = new 
        ProcessStartInfo("java.exe",
                      "-jar D:\\yourjarname.jar "+parameter);
        ProcessInfo.CreateNoWindow = true;
        ProcessInfo.UseShellExecute = false;
        // redirecting standard output and error
        ProcessInfo.RedirectStandardError = true;
        ProcessInfo.RedirectStandardOutput = true;

        process = Process.Start(ProcessInfo);
  
        process.WaitForExit();

       //Reading output and error
        output = process.StandardOutput.ReadToEnd();
        error = process.StandardError.ReadToEnd();

        exitcode = process.ExitCode;
        richTextBox1.Text=output;
        MessageBox.Show("Error:"+error);
        //Exit code '0' denotes success and '1' denotes failure
        MessageBox.Show("Exit Code:"+exitcode);
        process.Close();
    }

Use the above method in your windows application and call the above method in a click event of a button or any other event that best suits your scenario. If you have any parameter to be passed to the jar file you can get it from the user and pass it through the 'parameter' variable coded above. Do not forget to rename the ProcessStartInfo method argument with your own jar file name.


In case if you want to run batch files from windows application, replace the ProcessStartInfo method invocation alone from the above code with this,


ProcessInfo = new ProcessStartInfo("cmd.exe", "/c" + "D:\\Extract_Data\\bschoolextract.bat " + parameter);


In either case the process is the same, but while executing jar files you are executing the jvm(java.exe)itself instead of your own custom exe. 

Tips
1. You should always use double quotes when passing a file or directory path as parameter to any jar or batch file to avoid misinterpretation of spaces in the path.
2. You should always escape double quotes with \ before and after it when you use the  parameter that has double quotes as a string variable in your C# program.

For example, if the command to execute your jar file is as follows,

java -jar myjar.jar "D:\\Extract Data"

then the "parameter" passed should be escaped in c# code as,

string parameter;
parameter=textBox1.Text;
parameter="\"+parameter+\"";




Subscribe to GET LATEST ARTICLES!


Related

Dot Net 766960166731487911

Post a Comment

  1. Very nice and simple explanation! Good one.

    ReplyDelete
  2. very nice and i am requesting you the how to achieve the same requirement for web applications because i am using solr for my website and i am manually executing jar using cmd prompt but i want to achieve when user login then automatically jar should execute.Thanks in advance and i hope u understand my requirement.

    ReplyDelete

emo-but-icon

SUBSCRIBE


item