Reputation: 888
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
Upvotes: 26
Views: 26887
Reputation: 30647
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.
Upvotes: 1
Reputation: 3457
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.
Upvotes: 26
Reputation: 37172
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.
Upvotes: 3
Reputation: 54854
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.
Upvotes: 11
Reputation: 28
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.
Upvotes: 1