5

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
  • 305
  • 3
  • 4
  • 13
  • I think you'd have to use COM? May be wrong? – Liam Jun 11 '12 at 09:15
  • @duffymo I don't think so. That article doesn't have any information about C# DLL. – rajshekhar Jun 11 '12 at 09:20
  • There is a Java API for Microsoft Documents project on Apache, [Apache POI](http://poi.apache.org/). Could this be an alternative to mixing Java and C#? (Granted, I have no experience from using _Apache POI_ myself.) – Anders Gustafsson Jun 11 '12 at 09:42
  • @AndersGustafsson I have done the feasibility study on using Apache POI. But, that doesn't quite properly fit in my requirments. – rajshekhar Jun 11 '12 at 09:55
  • @Raj There are a few Java/COM interop libraries available, such as JACOB and j-interop. More info in [this SO answer](http://stackoverflow.com/a/2494741/650012). Could this be of any help? – Anders Gustafsson Jun 12 '12 at 13:52

2 Answers2

2

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)

olldman
  • 386
  • 1
  • 9
0

you may have to use JNI.

JNI is an acronym of Java Native Interface. Using JNI, we can call functions which are written in other languages from Java

have a look at the below

http://www.codeproject.com/Articles/2876/JNI-Basics-1

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • Thanks for sharing the link. But, that article talks about C/C++ DLLs. I need information about DLLs written in C#. – rajshekhar Jun 11 '12 at 09:23
  • 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 – Bjoern May 23 '14 at 11:11