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

<p>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. </p>

<p>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.</p>

<hr>

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

[{'answer_id': 50406, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 3, 'selected': False, 'text': "<p>If it's short, I think you're better off re-writing the code in java.  Downloading one 50Mb runtime is bad enough.</p>\n"}, {'answer_id': 50408, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 0, 'selected': False, 'text': "<p>I would rewrite it if it's not too much trouble.\nThe web service would work, but it seems like that would be a lot of overhead just to reuse a little code.</p>\n"}, {'answer_id': 50424, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 5, 'selected': False, 'text': '<p>You would use the Java Native Interface to call your C# code compiled into a DLL.</p>\n\n<p>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.</p>\n\n<p>Here is a highlevel overview of it:</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="noreferrer">http://en.wikipedia.org/wiki/Java_Native_Interface</a></p>\n\n<p>Your other option would be to create a COM assembly from the C# code and use J-Interop to invoke it.</p>\n\n<p><a href="http://sourceforge.net/projects/j-interop/" rel="noreferrer">http://sourceforge.net/projects/j-interop/</a></p>\n'}, {'answer_id': 50440, 'author': 'Tom Carter', 'author_id': 2839, 'author_profile': 'https://Stackoverflow.com/users/2839', 'pm_score': 4, 'selected': True, 'text': '<p>There is an IL to Java Bytecode compiler <a href="http://dev.mainsoft.com/Default.aspx?tabid=130" rel="nofollow noreferrer">GrassHopper</a> which may be of use to you.  I\'ve never tried it though.</p>\n\n<p>I\'d look at rewriting your code in Java though</p>\n\n<p>EDIT: Note that Grasshopper seems to be no longer available.</p>\n'}, {'answer_id': 50676, 'author': 'Jakub Šturc', 'author_id': 2361, 'author_profile': 'https://Stackoverflow.com/users/2361', 'pm_score': 2, 'selected': False, 'text': '<p>If you do not want to rewrite hadle it as an <a href="http://en.wikipedia.org/wiki/Inter-process_communication" rel="nofollow noreferrer">Inter-process communication</a> and choose one of following:</p>\n\n<ul>\n<li>Named pipes</li>\n<li>Sockets </li>\n<li>SOAP</li>\n</ul>\n'}, {'answer_id': 52577, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 2, 'selected': False, 'text': '<p>We used <a href="http://jnbridge.com" rel="nofollow noreferrer">JNBridge</a> for this, and it worked great.  It handles Java->.NET and vice versa, all in-proc.</p>\n'}, {'answer_id': 774505, 'author': 'Gili', 'author_id': 14731, 'author_profile': 'https://Stackoverflow.com/users/14731', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.infoq.com/articles/in-process-java-net-integration" rel="nofollow noreferrer">http://www.infoq.com/articles/in-process-java-net-integration</a> 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 <a href="http://sourceforge.net/projects/jace/" rel="nofollow noreferrer">Jace</a> if it works well.</p>\n'}, {'answer_id': 1655173, 'author': 'Pavel Savara', 'author_id': 129124, 'author_profile': 'https://Stackoverflow.com/users/129124', 'pm_score': 5, 'selected': False, 'text': '<p>I am author of <a href="http://jni4net.sf.net/" rel="noreferrer">jni4net</a>, 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.</p>\n'}, {'answer_id': 2400059, 'author': 'codenheim', 'author_id': 257090, 'author_profile': 'https://Stackoverflow.com/users/257090', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>If your C# app needs to call Java, compile a special Java main that takes appropriate command line args and returns text output.</p>\n\n<p>It the oldest, simplest method.</p>\n'}, {'answer_id': 55355810, 'author': 'Starblazer2078', 'author_id': 11254002, 'author_profile': 'https://Stackoverflow.com/users/11254002', 'pm_score': 0, 'selected': False, 'text': '<p>You can call your c# classes (compiled in a dll) via a bridging library, various libraries are available, every one with his characteristics. <strong>JNBridge</strong> generate proxy classes that you can call to manage the code in java classes. <strong>JCOBridge</strong> let you load your c# classes and use it from java using the invoke mechanism, also <strong>javonet</strong> let you import java classes and call java code using the invoke mechanism. <br>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.<br></p>\n\n<p><strong>Links</strong>:<br>\n<a href="http://www.jnbridge.com/" rel="nofollow noreferrer">jnbridge</a> java-.NET bridge Developer and Deployment license schema with 30 day free trial<br>\n<a href="https://www.jcobridge.com" rel="nofollow noreferrer">jcobridge</a> java-.NET bridge Developer and Deployment license schema with unlimited Trial<br>\n<a href="https://www.javonet.com/" rel="nofollow noreferrer">javonet</a> java-.NET bridge Research and Professional license schema with 30-day unlimited Trial after sign-up</p>\n'}]
['https://Stackoverflow.com/questions/50398', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5208/']