26

I'm not looking for the usual answer like Web-services. I'm looking for a light solution to be run in the same machine.

Edit: I'm looking for way in Java to call .NET methods

0

6 Answers 6

26

I am author of jni4net, open source interprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

Sign up to request clarification or add additional context in comments.

3 Comments

Esta libreria es maravillosa! with Reflection is all!
This library seems to be a good idea, but the status is said to be "alpha" and the latest update on the homepage seems to be from 2011 :-(
is it compatible with Mono?
11

I believe Java can talk to COM and .NET can expose COM interfaces. So that may be a very light weight solution that doesn't require any 3rd party. There is also the option of using sockets to communicate between the programs which wouldn't require a heavy instance of IIS to be installed on the machine.

3 Comments

Exactly. jacob.dll + jacob.jar
May I ask what is COM in this case ?
@Xitrum Component Object Model -- MS's standard on Windows platform for code reuse at binary level. Personally, I think it is a fascinating technology.
5

Have you looked at IKVM?

2 Comments

I'm looking for Java to Call .Net methods
You could run the Java code within IKVM though - that's the point. If you really want to be running a "normal" JVM, have a look at the Eclipse project and how they integrated WPF - that had both a JVM and a CLR running in the same process, I believe. It's likely to be pretty messy though.
3

Hve you looked into the Java Native Interface?

What I have done in the past is to write a C library, which is callable from both Java and .NET (and also COM, NSIS scripting language, and other technologies).

The JNI would work well if you only want to expose a few methods, but it might get unwieldy if you wanted to, for example, expose the entire .NET framework, because of the number of header files and wrapper methods you would need to create.

2 Comments

It is sooo easy to make evil mistakes on JNI, but it is definitively a workable solution. And as long as JNI defines some simple bridge, nothing too bad should happen
Easy is an understatement!! With the NSIS bridge where the arguments are passed in on a stack structure (as I recall), we managed to get some lovely memory leaks!
1

We tried IKVM in our production environment but it kept crashing. We use JNBridge which is a commercial product but is very stable and performs well in our ASP.NET environment.

Comments

1

If you're willing to run your Java code under .NET (or Mono), it is possible to do this using IKVM.

Because (as far as I know) current Java compilers don't read .NET assemblies, there are 2 steps. First you need to generate a stub JAR containing dummy classes and methods with the signatures of the .NET assembly you want to use, so that javac (or your IDE) knows what methods are available. For example, if you want to use something in the main .NET standard library from Java, run

ikvmstub mscorlib

which generates the stub mscorlib.jar. (It found the Mono mscorlib.dll assembly for me automatically under Linux, but if it fails you may have to give the full path to the DLL.)

You can then write a Java file that uses it, e.g. (based on the example from the IKVM documentation):

import cli.System.IO.Directory;
public class IKVMTest {
    public static void main(String[] args) {
        for(String file : Directory.GetFiles(".")) // From .NET standard library
            System.out.println(file);              // From Java standard library
    }
}

Note that CLI packges are prefixed with cli., hence the import cli.System instead of just System.

To compile, include the stub JAR on the classpath:

javac -classpath mscorlib.jar IKVMTest.java

Since Java linking occurs at runtime, the output class files use the methods of the desired names and signatures, but you can swap out the dummy stub methods with the real .NET methods when run under IKVM:

ikvm IKVMTest

and it will print the files in the current directory by calling the .NET methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.