37

Does anyone have a good solution for integrating some C# code into a java application?

The code is small, so I could re-write in java, but I would rather reuse the code if possible. Don't repeat yourself, etc.

Also, I know I can expose the C# as a web service or whatever, but it has some security/encryption stuff in there, so I would rather keep it tightly integrated if possible.


Edit: It's going to be on a server-based app, so "downloading" another runtime is irrelevant.

1
  • If its Server Based, what is the platform? I have no idea how well things like COM interop work with Mono, so that's another factor to consider. Commented Sep 8, 2008 at 19:10

10 Answers 10

33

You would use the Java Native Interface to call your C# code compiled into a DLL.

If its a small amount of C#, it would be much easier to port it to Java. If its a lot, this might be a good way to do it.

Here is a highlevel overview of it:

http://en.wikipedia.org/wiki/Java_Native_Interface

Your other option would be to create a COM assembly from the C# code and use J-Interop to invoke it.

http://sourceforge.net/projects/j-interop/

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

Comments

23

I am author of jni4net, open source intraprocess 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.

1 Comment

That looks pretty cool, did you ever find a way to support both Linux and Windows?
7

If it's short, I think you're better off re-writing the code in java. Downloading one 50Mb runtime is bad enough.

Comments

5

There is an IL to Java Bytecode compiler GrassHopper which may be of use to you. I've never tried it though.

I'd look at rewriting your code in Java though

EDIT: Note that Grasshopper seems to be no longer available.

2 Comments

The Grass hopper link is broken.
Probably no longer available
3

We used JNBridge for this, and it worked great. It handles Java->.NET and vice versa, all in-proc.

1 Comment

Yes, this is a commercial product.
2

If you do not want to rewrite hadle it as an Inter-process communication and choose one of following:

  • Named pipes
  • Sockets
  • SOAP

Comments

1

You can call your c# classes (compiled in a dll) via a bridging library, various libraries are available, every one with his characteristics. JNBridge generate proxy classes that you can call to manage the code in java classes. JCOBridge let you load your c# classes and use it from java using the invoke mechanism, also javonet let you import java classes and call java code using the invoke mechanism.
All the explored solutions are commercial solutions that let you call java code from .NET and vice-versa with graphical user interface integration and other amenities.

Links:
jnbridge java-.NET bridge Developer and Deployment license schema with 30 day free trial
jcobridge java-.NET bridge Developer and Deployment license schema with unlimited Trial
javonet java-.NET bridge Research and Professional license schema with 30-day unlimited Trial after sign-up

Comments

0

I would rewrite it if it's not too much trouble. The web service would work, but it seems like that would be a lot of overhead just to reuse a little code.

Comments

0

http://www.infoq.com/articles/in-process-java-net-integration suggests running CLR and JVM in the same process space and passing calls back and forth. It sounds very efficient. I'm going to give it a try and integrate it into Jace if it works well.

Comments

0

If it is a piece of code that is exposable as a command line utility, I just make the other host language use a system call to execute the utility.

If your C# app needs to call Java, compile a special Java main that takes appropriate command line args and returns text output.

It the oldest, simplest method.

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.