Calling C# dll from Java

12,304

We use ICE to communicate between .NET and Java apps. It is not exactly what you need but might help.

Also I'd recommend googling ".NET Java bridge" - there are several frameworks for this (jni4net, JNBridge etc)

Share:
12,304
rajshekhar
Author by

rajshekhar

A passionate Java developer, a Spring geek and RESTful nerd!

Updated on June 04, 2022

Comments

  • rajshekhar
    rajshekhar almost 2 years

    I am a Java developer. But, for some reason I have to take the help of C# to accomplish my task. I have below mentioned C# code which is used to create a DLL. That DLL has to be used in my Java program to do the needful.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    
    namespace Yrl.Tracingtool
    {
    public class DocxUtil
    {
        public Application Jump(string fileName)
        {
    
            object fileNameAsObject = (object)fileName;
            Application wordApplication;
            try
            {
                wordApplication = new Application();
                object readnly = false;
                object missing = System.Reflection.Missing.Value;
                wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readnly, ref missing,
                                                ref missing, ref missing, ref missing, ref missing,
                                                ref missing, ref missing, ref missing, ref missing,
                                                ref missing, ref missing, ref missing, ref missing);
                object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
                object count = 3;
    
                wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
    
                return wordApplication;
            }
            catch (Exception ex)
            {
                //LogEntry log = new LogEntry();
                //log.Categories.Add("Trace");
                //log.Message = ex.ToString();
                //Logger.Write(log, "Trace");
                throw new System.IO.FileLoadException("File cannot be opened");
            }
            finally
            {
                wordApplication = null;
            }
        }
    }
    }
    

    I have checked this forum and other forums too, but most of them talk about using a C++ or C DLL file in a JNI call. If anyone is having knowledge of calling C# DLL from Java please let me know.

  • rajshekhar
    rajshekhar almost 12 years
    Thanks for sharing the link. But, that article talks about C/C++ DLLs. I need information about DLLs written in C#.
  • Bjoern
    Bjoern almost 10 years
    Do you have an example for that? I agree to Raj, the C# DLLs are different than native Windows DLLs. I am actually searching for a tool to combine a Java and .NET project and if I could use JNI it might be a suitable solution. Thanks