Two Way Communication with JNI4NET - Call C# from JAVA then callback to JAVA from C#

338 views
Skip to first unread message

JA

unread,
Jan 18, 2020, 4:21:21 AM1/18/20
to jni4net
Okay, 

I am trying to make the most simple example in the work using JNI4NET but can't do it and need help. 

Here is the entire C# application:

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


namespace JNICSharpTestApp
{
   
public class JNICSharpTest
   
{
       
public void HelloWorldFromCSharp()
       
{
           
Console.WriteLine("Hi Java, this is C# talking!!!");
       
}


       
public void CallBackToJAVA()
       
{
            JAVACLASS java
= new JAVACLASS();
            java
.HelloWorldJAVA();
       
}
   
}
}



Here is the entire JAVA application:

package jnijavatestapp;
import java.io.IOException;
import jnicsharptestapp.JNICSharpTest;
import net.sf.jni4net.Bridge;


public class JNIJavaTestApp
{


   
public static void main(String[] args) throws IOException {
       
InitJNI4Net();
       
JNICSharpTest test = new JNICSharpTest();
       test
.HelloWorldFromCSharp();
   
}    
   
   
public static void InitJNI4Net() throws IOException{
       
Bridge.init();
        java
.io.File file = new java.io.File("..\\COMMON_RESOURCES\\work\\JNICSharpTestApp.j4n.dll");
       
Bridge.LoadAndRegisterAssemblyFrom(file);
   
}
}


and another simple JAVA class:

package jnijavatestapp;


public class JAVACLASS
{
   
public void HelloWorldJAVA()    
   
{
       
System.out.println("Hi C# this is JAVA Talking!!");
   
}
}


All I want to be able to do is call the C# code from JAVA but ALSO CALL the JAVA code back from C#. Is this possible using JNI4NET? I want TWO WAY communication. But it seems that no matter what I do with the build order I can't get it to work. 

Here is what I'm doing: 

1.) Build C# Project
2.) Generate Proxies for C# app:

@echo off


cd COMMON_RESOURCES


echo
.
echo
Running ProxyGen.exe for C#...
..\JNI4NET\bin\proxygen.exe work\JNICSharpTestApp.dll -wd work


echo
.
echo
Building C#...
cd work
build


3.) Run build (called in above .bat)
4.) Generate JAVA Proxies:

@echo off
copy JNI4NET
\lib\*.* COMMON_RESOURCES\work /y
copy
JNIJavaTestApp\dist\JNIJavaTestApp.jar COMMON_RESOURCES\work /y


cd COMMON_RESOURCES


echo
.
echo
Running ProxyGen.exe for JAVA...
..\JNI4NET\bin\proxygen.exe ..\JNIJavaTestApp\dist\JNIJavaTestApp.jar -wd work


echo
.
echo
Building JAVA...
cd work
build

5.) Build again (called in above script also)

Messing around with above order I am able to call C# from JAVA but can never call JAVA from C# without it crashing. 
Can someone help!?!



Björn Haverland

unread,
Dec 1, 2023, 8:11:56 AM12/1/23
to jni4net
Did you come up with a working solution?
Reply all
Reply to author
Forward
0 new messages