Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 967233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:16:58+00:00 2026-05-16T02:16:58+00:00

I am using jni4net to access Java code from within a C# application, and

  • 0

I am using jni4net to access Java code from within a C# application, and vice-versa. jni4net uses reflection to generate JNI code proxies, so obviously one of the limitations is that your Java and C# code have to compile in order to build proxies.

Unfortunately this can result in a catch-22 problem. Consider:

C# class X uses Java class Y
Java class Y uses C# class X

Neither can be compiled, so the well established workaround is to take one of the classes (either X or Y), strip it down to its bare signature and get it to compile and then generate the proxy from the compiled skeleton. You can then replace the stripped class with the original and go on your merry way.

This seems like an ugly approach to me and I believe there should be a better way. An obvious solution would be to tell the compiler (either C# or Java, it really does not matter which) to ignore references to the missing class.

Is ignoring references to a certain missing class feasible for either the C# or Java compilers? Is there a better way to do this (and no, I am not open to considering sockets or anything of that nature; I require true interop between .NET and Java)?

Example code was requested:

Example code with jni4net bridge code removed for clarity. IA and IB are simple interfaces also not included.

Java:

public class A implements IA  
{  
    public void m1()  
    {  
        System.out.println("m1 called");  
    }  

    public static void main (String args[])  
    {  
        IB b = new B();  
        b.m2(new A());  
    }  
}  

C#:

public class B : IB  
{  
    public void m2(IA a)  
    {  
        a.m1();  
        A a2 = new A();  
        a2.m1();  
    }  
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:16:59+00:00Added an answer on May 16, 2026 at 2:16 am

    I ended up solving the problem on my own. It did not involve my original hunch, so I changed the title of this problem to better reflect the actual issue.

    While the jni4net package has a lot of useful examples, there did not seem to be any good ones showing object passing in both directions without some strange build gymnastics (see original problem statement). I figured out how to do it and present the solution here along with the commands required to build the result.

    The easiest way to get this running is to set up jni4net as the instructions say and drop all of this stuff into a new sub-directory of the sample directory that comes with the jni4net package.

    First the C# code:

    src/test/left.cs

    namespace test
    {
        using System;
        using net.sf.jni4net;
        using test;
    
        public class left : iright
        {
            public static void Main(String[] args)
            {
                left l = new left();
                l.sendMeToRight();
            }
    
            public left()
            {
                Console.WriteLine("left side constructed...");
            }
    
            public void sendMeToRight()
            {
                BridgeSetup bridgeSetup = new BridgeSetup();
                bridgeSetup.AddAllJarsClassPath(".");
                Bridge.CreateJVM(bridgeSetup);
                Bridge.RegisterAssembly(typeof(right).Assembly);
    
                Console.WriteLine("Sending myself to right.");
                right il = new right(this);
                il.announceMyself();
            }
    
            public void announceMyself()
            {
                Console.WriteLine("Hello from the left side...");
            }
        }
    }
    

    And now the Java code:

    src/test/iright.java

    package test;
    
    public interface iright
    {
        void announceMyself();
    }
    

    src/test/right.java

    package test;
    
    public class right implements iright
    {
        public right(iright f)
        {
            System.out.println("right side constructed... ");
        f.announceMyself();
        }
    
        public void announceMyself()
        {
            System.out.println("Hello from the right side...");  
        }    
    }
    

    And finally the build script (I built it to run in cygwin, and on very short notice, so alter as needed.):

    @echo off
    rm -rf build
    mkdir build
    mkdir build\test
    copy ..\..\lib\*.* build
    
    echo Compile base classes.
    javac src/test/*.java
    mv src/test/*.class build/test
    
    echo Creating jar file.
    jar cvf build\lr.jar -C build test/iright.class -C build test/right.class
    rm build/test/*.class
    
    echo Generating proxies.
    ..\..\bin\proxygen.exe build\lr.jar -wd build
    
    echo Compiling derived proxy classes.
    javac -cp build\lr.jar;build\jni4net.j-0.8.0.0.jar build\jvm\test\iright_.java build\jvm\test\right_.java
    
    echo Packing compiled derived proxy classes.
    jar cvf build\lr.j4n.jar -C build\jvm test\__iright.class -C build\jvm test\iright_.class -C build\jvm test\right_.class
    
    echo Generating derived proxy DLL.
    cd build
    csc.exe /nologo /warn:0 /t:library /out:lr.j4n.dll /recurse:clr\*.cs /reference:"c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll" /reference:jni4net.n-0.8.0.0.dll
    
    echo Generating product executable.
    csc.exe /nologo /warn:0 /out:demo.exe /target:exe /reference:jni4net.n-0.8.0.0.dll /reference:lr.j4n.dll ..\src\test\left.cs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Java,I have to fetch multiple sets of values from an XML file to
Using Java, how can I extract all the links from a given web page?
Using mercurial, I've run into an odd problem where a line from one committer
Using Android TelephonyManager an application can obtain the state of data activity over the
Using php/html, I want to retrieve email addresses (plus other information) from MySQL and
(Using MVC 2) From inside my controller action, I need to display the url:
Using Yii, I want to delete all the rows that are not from today.
Using EF Code First I have an model object that has multiple properties that
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.