jni4net return value from c# to java

0

I'm using jni4net to call a library i made in C# (using visual studio 2013) from Java (Eclipse). Here is the code of my C# library :

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

namespace ClassLibrary2
{
public class Class1
{   

    // Simple method that just prints a hard-coded string.
    public void Hello()
    {
        Console.WriteLine("Hello Java, from .NET!");
    }

    // Parameterized method that accepts a string
    public void Repeat(string line)
    {
        Console.WriteLine(line);
    }

    public int mult(int n)
    {
        return n*2;
    }
}
}

and here is my java code :

import net.sf.jni4net.Bridge;
import net.sf.jni4net.Ref;

public class jni {

public static void main(String[] args) {
    try {
        //Bridge.setVerbose(true);
        Bridge.init();
        Bridge.LoadAndRegisterAssemblyFrom(new File("ClassLibrary2.j4n.dll"));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Class1 c = new Class1();    

    c.Hello();
    c.Repeat("pippo");
    c.mult(2);        
}

}

I'm using "proxygen" script that was included into jni4net packet to wrap mt C# code into Java.

when i call the functions "Hello" and "Repeat" everything works just fine. I get the correct output, but when calling the function "mult" I get the following error :

# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (0xe0434352), pid=6808, tid=7100
#
# JRE version: Java(TM) SE Runtime Environment (8.0_11-b12) (build 1.8.0_11-    b12)
# Java VM: Java HotSpot(TM) Client VM (25.11-b03 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [KERNELBASE.dll+0xc42d]
#
# Failed to write core dump. Minidumps are not enabled by default on client  versions of Windows
#
# An error report file with more information is saved as:
# C:\ws\DLL\jni4net\hs_err_pid6808.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

It also generates a log file, here it is :

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

Current thread (0x00dcc000):  JavaThread "main" [_thread_in_native, id=7100, stack(0x00e60000,0x00eb0000)]

siginfo: ExceptionCode=0xe0434352, ExceptionInformation=0x80131513 0x00000000 0x00000000 0x00000000 0x72450000 

Registers:
EAX=0x00eae5ec, EBX=0x00000005, ECX=0x00000005, EDX=0x00000000
ESP=0x00eae5ec, EBP=0x00eae63c, ESI=0x00eae6ac, EDI=0x00000001
EIP=0x7566c42d, EFLAGS=0x00000202

Top of Stack: (sp=0x00eae5ec)
0x00eae5ec:   e0434352 00000001 00000000 7566c42d
0x00eae5fc:   00000005 80131513 00000000 00000000
0x00eae60c:   00000000 72450000 00000001 00000000
0x00eae61c:   006d235c 001f0178 00000000 001f0150
0x00eae62c:   72461865 006d235c 00239dc0 00eae5f4
0x00eae63c:   00eae6e0 725a2a42 e0434352 00000001
0x00eae64c:   00000005 00eae6ac d1e7f1fc 1665aae0
0x00eae65c:   002a7da8 00eae798 00239dc0 00000001 

Instructions: (pc=0x7566c42d)
0x7566c40d:   02 50 ff 75 14 8d 45 c4 50 e8 57 ad 02 00 83 c4
0x7566c41d:   0c eb 03 89 45 c0 8d 45 b0 50 ff 15 5c 11 66 75
0x7566c42d:   c9 c2 10 00 cc cc cc cc cc 8b ff 55 8b ec 56 8b
0x7566c43d:   75 08 83 fe f4 72 18 83 fe f6 77 13 8d 45 08 50 


Register to memory mapping:

EAX=0x00eae5ec is pointing into the stack for thread: 0x00dcc000
EBX=0x00000005 is an unknown value
ECX=0x00000005 is an unknown value
EDX=0x00000000 is an unknown value
ESP=0x00eae5ec is pointing into the stack for thread: 0x00dcc000
EBP=0x00eae63c is pointing into the stack for thread: 0x00dcc000
ESI=0x00eae6ac is pointing into the stack for thread: 0x00dcc000
EDI=0x00000001 is an unknown value


Stack: [0x00e60000,0x00eb0000],  sp=0x00eae5ec,  free space=313k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [KERNELBASE.dll+0xc42d]
C  [clr.dll+0x152a42]
C  [clr.dll+0x2255ef]
C  [clr.dll+0x22de52]
C  [clrjit.dll+0x28df]
C  [clrjit.dll+0x290b]
C  [clrjit.dll+0x1d53]
C  [clrjit.dll+0x1f48]
C  [clrjit.dll+0x3405]
C  [clrjit.dll+0x3635]
C  [clrjit.dll+0x3823]
C  [clrjit.dll+0x36f6]
C  [clrjit.dll+0x385f]
C  [clr.dll+0x34710]
C  [clr.dll+0x347a9]
C  [clr.dll+0x347eb]
C  [clr.dll+0x33684]
C  [clr.dll+0x33920]
C  [clr.dll+0x41e5e]
C  [clr.dll+0x150b6]
C  [clr.dll+0x4279]
C  0x151d4d03
j  classlibrary2.Class1.mult(I)I+0
j  jni4net.jni.main([Ljava/lang/String;)V+43
v  ~StubRoutines::call_stub
V  [jvm.dll+0x142295]
V  [jvm.dll+0x2073ae]
V  [jvm.dll+0x14232e]
V  [jvm.dll+0xce7c7]
V  [jvm.dll+0xd6d6f]
C  [javaw.exe+0x20d8]
C  [javaw.exe+0xab91]
C  [javaw.exe+0xac1b]
C  [kernel32.dll+0x1338a]
C  [ntdll.dll+0x39f72]
C  [ntdll.dll+0x39f45]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  classlibrary2.Class1.mult(I)I+0
j  jni4net.jni.main([Ljava/lang/String;)V+43
v  ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
  0x15c0ac00 JavaThread "Thread-0" daemon [_thread_in_native, id=5404, stack(0x15ef0000,0x15f40000)]
  0x007b9000 JavaThread "Service Thread" daemon [_thread_blocked, id=5452, stack(0x15ad0000,0x15b20000)]
  0x00788400 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=7312, stack(0x04700000,0x04750000)]
  0x00787400 JavaThread "Attach Listener" daemon [_thread_blocked, id=6992, stack(0x01240000,0x01290000)]
  0x00779400 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=568, stack(0x15940000,0x15990000)]
  0x00776c00 JavaThread "Finalizer" daemon [_thread_blocked, id=4412, stack(0x15390000,0x153e0000)]
  0x00771c00 JavaThread "Reference Handler" daemon [_thread_blocked, id=5608, stack(0x151f0000,0x15240000)]
=>0x00dcc000 JavaThread "main" [_thread_in_native, id=7100, stack(0x00e60000,0x00eb0000)]

Other Threads:
  0x0076fc00 VMThread [stack: 0x04790000,0x047e0000] [id=5488]
  0x007ba800 WatcherThread [stack: 0x15a50000,0x15aa0000] [id=6180]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap:
 def new generation   total 4928K, used 3145K [0x04800000, 0x04d50000, 0x09d50000)
  eden space 4416K,  71% used [0x04800000, 0x04b12798, 0x04c50000)
  from space 512K,   0% used [0x04c50000, 0x04c50000, 0x04cd0000)
  to   space 512K,   0% used [0x04cd0000, 0x04cd0000, 0x04d50000)
 tenured generation   total 10944K, used 0K [0x09d50000, 0x0a800000, 0x14800000)
   the space 10944K,   0% used [0x09d50000, 0x09d50000, 0x09d50200, 0x0a800000)
 Metaspace       used 532K, capacity 2664K, committed 2752K, reserved 4480K

Card table byte_map: [0x00ce0000,0x00d70000] byte_map_base: 0x00cbc000

Polling page: 0x000c0000

CodeCache: size=32768Kb used=748Kb max_used=748Kb free=32020Kb
 bounds [0x02700000, 0x027c0000, 0x04700000]
 total_blobs=203 nmethods=59 adapters=76
 compilation: enabled

Compilation events (10 events):
Event: 0.371 Thread 0x00788400   54             java.nio.DirectLongBufferU::ix (10 bytes)
Event: 0.371 Thread 0x00788400 nmethod 54 0x027ba008 code [0x027ba100, 0x027ba190]
Event: 0.382 Thread 0x00788400   56             java.lang.AbstractStringBuilder::expandCapacity (50 bytes)
Event: 0.383 Thread 0x00788400 nmethod 56 0x027ba448 code [0x027ba560, 0x027ba798]
Event: 0.400 Thread 0x00788400   57             java.lang.String::endsWith (17 bytes)
Event: 0.401 Thread 0x00788400 nmethod 57 0x027ba8c8 code [0x027ba9d0, 0x027baa7c]
Event: 0.424 Thread 0x00788400   58             java.lang.String::indexOf (25 bytes)
Event: 0.424 Thread 0x00788400 nmethod 58 0x027bab48 code [0x027bac50, 0x027bad1c]
Event: 0.424 Thread 0x00788400   59             java.util.HashMap::hash (20 bytes)
Event: 0.424 Thread 0x00788400 nmethod 59 0x027bae08 code [0x027baf00, 0x027baf9c]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (10 events):
Event: 0.423 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04adeeb0) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.423 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04ae0e20) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.424 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04ae2d68) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.425 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04ae4b50) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.425 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04afceb0) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.426 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04afef48) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.426 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04b00ef0) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.429 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04b03080) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.430 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04b05ee0) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]
Event: 0.444 Thread 0x00dcc000 Exception <a 'java/security/PrivilegedActionException'> (0x04b099f8) thrown at [D:\re\puppet\workspace\8-2-build-windows-i586-cygwin\jdk8u11\648\hotspot\src\share\vm\prims\jvm.cpp, line 1248]

Events (10 events):
Event: 0.425 loading class system/IFormattable_
Event: 0.425 loading class system/IFormattable_ done
Event: 0.426 loading class system/__IFormattable
Event: 0.426 loading class system/__IFormattable done
Event: 0.429 loading class system/NotSupportedException
Event: 0.429 loading class system/NotSupportedException done
Event: 0.430 loading class system/NullReferenceException
Event: 0.430 loading class system/NullReferenceException done
Event: 0.444 loading class classlibrary2/Class1
Event: 0.444 loading class classlibrary2/Class1 done


Dynamic libraries:
0x012d0000 - 0x01300000 	C:\Program Files (x86)\Java\jre8\bin\javaw.exe
0x77710000 - 0x77890000 	C:\Windows\SysWOW64\ntdll.dll
0x75fc0000 - 0x760d0000 	C:\Windows\syswow64\kernel32.dll
0x75660000 - 0x756a7000 	C:\Windows\syswow64\KERNELBASE.dll
0x760d0000 - 0x76170000 	C:\Windows\syswow64\ADVAPI32.dll
0x757a0000 - 0x7584c000 	C:\Windows\syswow64\msvcrt.dll
0x75770000 - 0x75789000 	C:\Windows\SysWOW64\sechost.dll
0x75560000 - 0x75650000 	C:\Windows\syswow64\RPCRT4.dll
0x750b0000 - 0x75110000 	C:\Windows\syswow64\SspiCli.dll
0x750a0000 - 0x750ac000 	C:\Windows\syswow64\CRYPTBASE.dll
0x75440000 - 0x75540000 	C:\Windows\syswow64\USER32.dll
0x752a0000 - 0x75330000 	C:\Windows\syswow64\GDI32.dll
0x75430000 - 0x7543a000 	C:\Windows\syswow64\LPK.dll
0x751a0000 - 0x7523d000 	C:\Windows\syswow64\USP10.dll
0x6f920000 - 0x6fabe000 	C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_41e6975e2bd6f2b2\COMCTL32.dll
0x75330000 - 0x75387000 	C:\Windows\syswow64\SHLWAPI.dll
0x75c30000 - 0x75c90000 	C:\Windows\system32\IMM32.DLL
0x75b60000 - 0x75c2c000 	C:\Windows\syswow64\MSCTF.dll
0x52640000 - 0x526ff000 	C:\Program Files (x86)\Java\jre8\bin\msvcr100.dll
0x68230000 - 0x685c7000 	C:\Program Files (x86)\Java\jre8\bin\client\jvm.dll
0x74d40000 - 0x74d47000 	C:\Windows\system32\WSOCK32.dll
0x753f0000 - 0x75425000 	C:\Windows\syswow64\WS2_32.dll
0x75540000 - 0x75546000 	C:\Windows\syswow64\NSI.dll
0x74cd0000 - 0x74d02000 	C:\Windows\system32\WINMM.dll
0x758e0000 - 0x758e5000 	C:\Windows\syswow64\PSAPI.DLL
0x6b830000 - 0x6b83c000 	C:\Program Files (x86)\Java\jre8\bin\verify.dll
0x68200000 - 0x68221000 	C:\Program Files (x86)\Java\jre8\bin\java.dll
0x681e0000 - 0x681f3000 	C:\Program Files (x86)\Java\jre8\bin\zip.dll
0x76520000 - 0x7716a000 	C:\Windows\syswow64\SHELL32.dll
0x75e60000 - 0x75fbc000 	C:\Windows\syswow64\ole32.dll
0x75280000 - 0x7528b000 	C:\Windows\syswow64\profapi.dll
0x52de0000 - 0x52dea000 	C:\ws\DLL\jni4net\jni4net.n.w32.v40-0.8.8.0.dll
0x74420000 - 0x7446a000 	C:\Windows\SYSTEM32\MSCOREE.DLL
0x743a0000 - 0x7441b000 	C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll
0x72450000 - 0x72aeb000 	C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
0x72370000 - 0x72443000 	C:\Windows\system32\MSVCR110_CLR0400.dll
0x711b0000 - 0x721de000 	C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\ce5f61c5754789df97be8dc991c47d07\mscorlib.ni.dll
0x72250000 - 0x72266000 	C:\Windows\system32\CRYPTSP.dll
0x72210000 - 0x7224b000 	C:\Windows\system32\rsaenh.dll
0x71060000 - 0x710dd000 	C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll
0x75ad0000 - 0x75b5f000 	C:\Windows\syswow64\OLEAUT32.dll
0x763d0000 - 0x7651a000 	C:\Windows\syswow64\urlmon.dll
0x75270000 - 0x75274000 	C:\Windows\syswow64\api-ms-win-downlevel-ole32-l1-1-0.dll
0x776e0000 - 0x776e4000 	C:\Windows\syswow64\api-ms-win-downlevel-shlwapi-l1-1-0.dll
0x75720000 - 0x75725000 	C:\Windows\syswow64\api-ms-win-downlevel-advapi32-l1-1-0.dll
0x75730000 - 0x75734000 	C:\Windows\syswow64\api-ms-win-downlevel-user32-l1-1-0.dll
0x75290000 - 0x75294000 	C:\Windows\syswow64\api-ms-win-downlevel-version-l1-1-0.dll
0x74e80000 - 0x74e89000 	C:\Windows\system32\version.DLL
0x75650000 - 0x75653000 	C:\Windows\syswow64\api-ms-win-downlevel-normaliz-l1-1-0.dll
0x75790000 - 0x75793000 	C:\Windows\syswow64\normaliz.DLL
0x76170000 - 0x763a2000 	C:\Windows\syswow64\iertutil.dll
0x758f0000 - 0x75ac6000 	C:\Windows\syswow64\WININET.dll
0x763b0000 - 0x763c7000 	C:\Windows\syswow64\USERENV.dll
0x705f0000 - 0x705f8000 	C:\Windows\system32\Secur32.dll
0x6c170000 - 0x6c174000 	C:\Windows\system32\api-ms-win-downlevel-advapi32-l2-1-0.dll
0x70000000 - 0x70012000 	C:\Windows\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll
0x70600000 - 0x70fa4000 	C:\Windows\assembly\NativeImages_v4.0.30319_32\System\17a393b77ae757f0768501fb95ff5af6\System.ni.dll
0x70520000 - 0x7052e000 	C:\Windows\system32\RpcRtRemote.dll
0x6f820000 - 0x6f90b000 	C:\Windows\system32\dbghelp.dll

VM Arguments:
jvm_args: -Dfile.encoding=Cp1252 
java_command: jni4net.jni
java_class_path (initial): C:\ws\DLL\jni4net\bin;C:\ws\DLL\jni4net\jni4net.j-0.8.8.0.jar;C:\ws\DLL\jni4net\ClassLibrary2.j4n.jar
Launcher Type: SUN_STANDARD

Environment Variables:
CLASSPATH=.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip
PATH=C:/Program Files (x86)/Java/jre8/bin/client;C:/Program Files (x86)/Java/jre8/bin;C:/Program Files (x86)/Java/jre8/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CodeBlocks\MinGW\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\ ;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\GitExtensions\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Users\Marco\Desktop\eclipse;
USERNAME=Marco
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel



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

OS: Windows 7 , 64 bit Build 7601 Service Pack 1

CPU:total 4 (2 cores per cpu, 2 threads per core) family 6 model 42 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, aes, clmul, ht, tsc, tscinvbit

Memory: 4k page, physical 4072380k(1064400k free), swap 8142896k(4029200k free)

vm_info: Java HotSpot(TM) Client VM (25.11-b03) for windows-x86 JRE (1.8.0_11-b12), built on Jun 16 2014 18:18:58 by "java_re" with MS VC++ 10.0 (VS2010)

time: Wed Feb 04 15:24:40 2015
elapsed time: 0 seconds

The problem is that I can't figure out how to return a value from c# library to Java...

I have also tried to modify the parameter of the function :

public int mult(ref int n)

and then in Java :

 Ref<Integer> a = new Ref<Integer>(4);
 c.mult(a);

but I seem to get the same error..How can I do?

Thanks in advice, Stefano

java
c#
jni4net

1 Answer

0

Usually this happens is you changed the signature of the method but forgot to re-generate it with proxygen and recompile the proxy dll/jar. If that doesn't help, please post the generated code as well.

answered on Stack Overflow Feb 4, 2015 by Pavel Savara

User contributions licensed under CC BY-SA 3.0