Integração de JAVA e C# usando JNI

Olá pessoal… estou tentando integrar o JAVA com o C# e estou tendo problemas com isso…

Criei minha classe JAVA “HelloWorld.java”:

public class HelloWorld {    
	public native void ola();
  
	static {
        System.loadLibrary("HelloWorldGen");
    } 
	
	public static void main (String[] args) {
        new HelloWorld().ola();  
    }
}

compilei a mesma e após gerei a header em C usando a compilação do JNI: javah -jni HelloWorld

HelloWorld.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    ola
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_ola
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

após isso gerei a classe em C#:

CSHelloWorld.cs

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

public class CSHelloWorld
{
	public CSHelloWorld() { }

	public void ola()
	{
		Console.Write("Olá pessoal, se chegar aqui é por que funcionou a integração C# e JAVA.");
	}
}

dai gerei o .netmodule dela

após gerei minha a lib c++ que faz a leitura do netmodule C#:

#using <mscorlib.dll>
#using "CSHelloWorld.netmodule"

using namespace System;

public __gc class HelloWorldC
{
    public:
		//
        CSHelloWorld __gc *t;
        HelloWorldC() {
			//Chama o método no C# 
            t = new CSHelloWorld();
        }
        
        void callCSHelloWorld() {
            t->ola();
        }
};

após gerei meu arquivo C que liga o header do JAVA com a lib C++ que faz a leitura do netmodule do C#
HelloWorld.cpp

#include "stdafx.h"
#include "HelloWorld.h"  
#include "HelloWorldC.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloWorld_ola(JNIEnv *env, jobject obj)   
 {  
	 // Instantiate the MC++ class.
    HelloWorldC* t = new HelloWorldC();
    // The actual call is made. 
    t->callCSHelloWorld();
 }  

feito a construção desses arquivos, gerei minha .dll “HelloWorldGen.dll”, copiei ela para a pasta do JAVA e fui executar minha classe do JAVA para ver se ia chamar meu método ola() no C#. Mas me ocorreu o seguinte erro quando tentei executar meu arquivo JAVA

[b]#

A fatal error has been detected by the Java Runtime Environment:

Internal Error (0xe0434352), pid=4228, tid=1240

JRE version: 6.0_20-b02

Java VM: Java HotSpot™ Client VM (16.3-b01 mixed mode, sharing windows-x86 )

Problematic frame:

C [KERNELBASE.dll+0x9617]

If you would like to submit a bug report, please visit:

http://java.sun.com/webapps/bugreport/crash.jsp

The crash happened outside the Java Virtual Machine in native code.

See problematic frame for where to report the bug.

--------------- T H R E A D ---------------

Current thread (0x00c19800): JavaThread “main” [_thread_in_native, id=1240, stack(0x00390000,0x003e0000)]

siginfo: ExceptionCode=0xe0434352, ExceptionInformation=0x80070002 0x00000000 0x00000000 0x00000000 0x63830000

Registers:
EAX=0x003dea58, EBX=0x00000005, ECX=0x00000005, EDX=0x00000000
ESP=0x003dea58, EBP=0x003deaa8, ESI=0x003deb04, EDI=0x00135128
EIP=0x75389617, EFLAGS=0x00000212

Top of Stack: (sp=0x003dea58)
0x003dea58: e0434352 00000001 00000000 75389617
0x003dea68: 00000005 80070002 00000000 00000000
0x003dea78: 00000000 63830000 02000059 003dea90
0x003dea88: 6384bc5e 62d7fd9c 003deaa0 638ba99c
0x003dea98: 62d748ec 0000014e 003deab0 63831bd0
0x003deaa8: 003deb38 638bac08 e0434352 00000001
0x003deab8: 00000005 003deb04 6b34c1c8 00000000
0x003deac8: 041eb798 003debc0 00135128 00000001

Instructions: (pc=0x75389617)
0x75389607: d1 ff ff 83 c4 0c 8d 45 b0 50 ff 15 6c 11 38 75
0x75389617: c9 c2 10 00 89 45 c0 eb ed 90 90 90 90 90 8b ff

Stack: [0x00390000,0x003e0000], sp=0x003dea58, free space=13a003de5a8k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [KERNELBASE.dll+0x9617]
C [clr.dll+0x8ac08]
C [clr.dll+0xfab0b]
C [clr.dll+0x2cbc8d]
C [clrjit.dll+0x7e0e]
C [clrjit.dll+0x1d342]
C [clrjit.dll+0x2d28]
C [clrjit.dll+0x2e58]
C [clrjit.dll+0x2ea2]
C [clrjit.dll+0x32a2]
C [clrjit.dll+0x3396]
C [clrjit.dll+0x34c9]
C [clrjit.dll+0x5e4b]
C [clr.dll+0x6f7f1]
C [clr.dll+0x6f87d]
C [clr.dll+0x6f8c3]
C [clr.dll+0x6f698]
C [clr.dll+0xd37d3]
C [clr.dll+0xd3980]
C [clr.dll+0x21f55]
C 0x04140842
C 0x040e182a
C 0x04140a1c
j HelloWorld.ola()V+0
j HelloWorld.main([Ljava/lang/String;)V+7
v ~StubRoutines::call_stub
V [jvm.dll+0xf049c]
V [jvm.dll+0x17fcf1]
V [jvm.dll+0xf051d]
V [jvm.dll+0xf9bc5]
V [jvm.dll+0x10181d]
C [java.exe+0x2155]
C [java.exe+0x85b4]
C [kernel32.dll+0x51194]
C [ntdll.dll+0x5b495]
C [ntdll.dll+0x5b468]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j HelloWorld.ola()V+0
j HelloWorld.main([Ljava/lang/String;)V+7
v ~StubRoutines::call_stub

--------------- P R O C E S S ---------------

Java Threads: ( => current thread )
0x01969000 JavaThread “Low Memory Detector” daemon [_thread_blocked, id=5760, stack(0x03d40000,0x03d90000)]
0x0195f800 JavaThread “CompilerThread0” daemon [_thread_blocked, id=472, stack(0x03cf0000,0x03d40000)]
0x0195c800 JavaThread “Attach Listener” daemon [_thread_blocked, id=1252, stack(0x03ca0000,0x03cf0000)]
0x01959800 JavaThread “Signal Dispatcher” daemon [_thread_blocked, id=2692, stack(0x03c50000,0x03ca0000)]
0x01955800 JavaThread “Finalizer” daemon [_thread_blocked, id=3036, stack(0x03c00000,0x03c50000)]
0x01950c00 JavaThread “Reference Handler” daemon [_thread_blocked, id=3904, stack(0x03bb0000,0x03c00000)]
=>0x00c19800 JavaThread “main” [_thread_in_native, id=1240, stack(0x00390000,0x003e0000)]

Other Threads:
0x0194f800 VMThread [stack: 0x03b60000,0x03bb0000] [id=316]
0x0197a800 WatcherThread [stack: 0x03d90000,0x03de0000] [id=5160]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
def new generation total 4928K, used 281K [0x23b50000, 0x240a0000, 0x290a0000)
eden space 4416K, 6% used [0x23b50000, 0x23b964a8, 0x23fa0000)
from space 512K, 0% used [0x23fa0000, 0x23fa0000, 0x24020000)
to space 512K, 0% used [0x24020000, 0x24020000, 0x240a0000)
tenured generation total 10944K, used 0K [0x290a0000, 0x29b50000, 0x33b50000)
the space 10944K, 0% used [0x290a0000, 0x290a0000, 0x290a0200, 0x29b50000)
compacting perm gen total 12288K, used 25K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 0% used [0x33b50000, 0x33b565f8, 0x33b56600, 0x34750000)
ro space 10240K, 51% used [0x37b50000, 0x3807ae00, 0x3807ae00, 0x38550000)
rw space 12288K, 54% used [0x38550000, 0x38be72d8, 0x38be7400, 0x39150000)

Dynamic libraries:
0x00400000 - 0x00424000 C:\Windows\system32\java.exe
0x77010000 - 0x7714c000 C:\Windows\SYSTEM32\ntdll.dll
0x76000000 - 0x760d4000 C:\Windows\system32\kernel32.dll
0x75380000 - 0x753ca000 C:\Windows\system32\KERNELBASE.dll
0x759e0000 - 0x75a80000 C:\Windows\system32\ADVAPI32.dll
0x75a80000 - 0x75b2c000 C:\Windows\system32\msvcrt.dll
0x75770000 - 0x75789000 C:\Windows\SYSTEM32\sechost.dll
0x760e0000 - 0x76181000 C:\Windows\system32\RPCRT4.dll
0x7c340000 - 0x7c396000 D:\Programas\Java\jre6\bin\msvcr71.dll
0x6d800000 - 0x6da97000 D:\Programas\Java\jre6\bin\client\jvm.dll
0x75e30000 - 0x75ef9000 C:\Windows\system32\USER32.dll
0x75d50000 - 0x75d9e000 C:\Windows\system32\GDI32.dll
0x756b0000 - 0x756ba000 C:\Windows\system32\LPK.dll
0x756c0000 - 0x7575d000 C:\Windows\system32\USP10.dll
0x735a0000 - 0x735d2000 C:\Windows\system32\WINMM.dll
0x75460000 - 0x7547f000 C:\Windows\system32\IMM32.DLL
0x76de0000 - 0x76eac000 C:\Windows\system32\MSCTF.dll
0x75060000 - 0x750ab000 C:\Windows\system32\apphelp.dll
0x6d7b0000 - 0x6d7bc000 D:\Programas\Java\jre6\bin\verify.dll
0x6d330000 - 0x6d34f000 D:\Programas\Java\jre6\bin\java.dll
0x6d290000 - 0x6d298000 D:\Programas\Java\jre6\bin\hpi.dll
0x75760000 - 0x75765000 C:\Windows\system32\PSAPI.DLL
0x6d7f0000 - 0x6d7ff000 D:\Programas\Java\jre6\bin\zip.dll
0x642b0000 - 0x642c1000 C:\Users\juliano\Documents\NetBeansProjects\integracao-c\src\teste\HelloWorldGen.dll
0x6f320000 - 0x6f36a000 C:\Windows\SYSTEM32\MSCOREE.DLL
0x0f9c0000 - 0x0fb32000 C:\Windows\system32\MSVCR100D.dll
0x6f2b0000 - 0x6f316000 C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll
0x755c0000 - 0x75617000 C:\Windows\system32\SHLWAPI.dll
0x63830000 - 0x63e9f000 C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
0x69a80000 - 0x69b3e000 C:\Windows\system32\MSVCR100_CLR0400.dll
0x62a60000 - 0x63823000 C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\246f1a5abb686b9dcdf22d3505b08cea\mscorlib.ni.dll
0x731d0000 - 0x731e0000 C:\Windows\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll
0x68390000 - 0x683f0000 C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll
0x5c0b0000 - 0x5c15a000 C:\Windows\Microsoft.NET\Framework\v4.0.30319\diasymreader.dll
0x76eb0000 - 0x7700c000 C:\Windows\system32\ole32.dll
0x621c0000 - 0x62a58000 C:\Windows\assembly\NativeImages_v4.0.30319_32\System\964da027ebca3b263a05cadb8eaa20a3\System.ni.dll

VM Arguments:
java_command: HelloWorld
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=D:\Programas\Java\jdk1.6.0_20
CLASSPATH=.;JAVA_HOME
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Microsoft SQL Server\90\Tools\binn;C:\Program Files\Microsoft SQL Server\100\Tools\Binn;C:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE;D:\Programas\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies;D:\Programas\QuickTime\QTSystem;D:\Programas\TortoiseSVN\bin;D:\Programas\Java\jdk1.6.0_20\bin;C:\Program Files\Microsoft Visual Studio 10.0\SDK\v3.5\Bin;C:\Windows\Microsoft.NET\Framework\v3.5;C:\Program Files\Microsoft Visual Studio 10.0\VC\bin;C:\Program Files\SSH Communications Security\SSH Secure Shell;D:\Programas\Java\jdk1.6.0_20\bin
USERNAME=juliano
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel

--------------- S Y S T E M ---------------

OS: Windows 7 Build 7600

CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 15 stepping 13, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3

Memory: 4k page, physical 3137080k(1132028k free), swap 6272396k(3933696k free)

vm_info: Java HotSpot™ Client VM (16.3-b01) for windows-x86 JRE (1.6.0_20-b02), built on Apr 12 2010 13:52:23 by “java_re” with MS VC++ 7.1 (VS2003)

time: Thu Dec 23 10:01:07 2010
elapsed time: 0 seconds[/b]

Alguem tem idéia do que possa ser?

Porquê não usa JNA? Gere sua DLL em C# e use JNA para acessar os métodos (sem envolver C++ no meio).

Um artigo sobre JNA: http://www.urubatan.com.br/chamando-metodos-nativos-dllso-em-java-sem-jni/

Melhor ainda é não usar nem JNI e nem JNA.

Considere:

  1. Usar algum protocolo de comunicação (pode ser feito por vc, ou algum standard, como o RMI);
  2. Usar trocas de arquivo;
  3. Usar pipes;
  4. Usar a fila de mensagens do SO.

O java não casa muito bem com o JNI (pelo menos, não sem muito suor), e os erros são meio exotéricos, como você mesmo pode perceber.

Obrigado pessoal pelas dicas, vou pesquisar um pouco mais e vou tentar evitar as JNI…
se conseguir resolver meu problema com as opções que me ofereceram, eu entro em contato…
Abraços