How To call Java Jar from C# Code

First I will say how to create a jar file for given java package using command prompt

You can create Manifest file using note pad by including below mentioned contents

                       Manifest-Version: 1.0
                       Main-Class: Main

Here manifest file is named as manifest.mf


Here Main is the main class of the java project. In my example java project it only contains Main.java with below code.
                      
                public class Main {

                             public static void main(String[] args) {
                                    // TODO code application logic here
                                       System.out.print("Test");
                                      }

                         }


Now you can create a jar using below command in command prompt

        jar cfm MyJar.jar manifest.mf *.class
Here MyJar.jar - name for jar file I want to create
        manifest.mf - name of Manifest File
        *.class - All the classes your java project contains

Now We consider How to call this Jar in C# code

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Diagnostics;


namespace BridgeFromCToJava
{
    class Program
    {
        private int ExecuteProcess(string cmd, string cmdParams, string workingDirectory, int timeout, out string stdOutput)
        {
            using (Process process = Process.Start(new ProcessStartInfo(cmd, cmdParams)))
            {
                process.StartInfo.WorkingDirectory = workingDirectory;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                stdOutput = process.StandardOutput.ReadToEnd();
                //process.WaitForExit(timeout);
                return process.ExitCode;
            }
        }
        static void Main(string[] args)
        {
            string stdOutput;
            int myTimeout = 5;
            int exitCode = new Program().ExecuteProcess(@"C:\jre1.6.0\bin\Java.exe"
  , "-jar MyJar.jar "
  , @"D:\JavaFiles\"
  , myTimeout
  , out stdOutput );
            Console.WriteLine(stdOutput);

        }
       
    }
}


Here ,
string cmd - path for the java.exe. because our jar file runs under the java.exe and so java.exe should run as  a separate process.
string cmdParams - parameters to run our jar file
string  workingDirectory - working directory in which our jar file locates
string stdOutput - to get the output of jar file

If you want to set arguments to run your jar then you can set those as follows

             -jar MyJar.jar Test

Comments

Popular posts from this blog

How to add standard and custom Metadata to PDF using iTextSharp

How to set and get Metadata to image using BitmapMetadata class in C#

How to generate direct line token to start a new conversation between your own client application and bot framework in Java