8

We've written a Java program which we are looking to use and interact with from C#. What are our options? Optimally it would be possible to compile the Java application as a library (.DLL) that we could reference from C# perhaps using P/Invoke. This, however, doesn't appear to be an option according to the first few searches online.

We opt to be able to use ASP.NET to built a search engine powered by the Java code, so if this opens up for any other options please let us know.

Kasper Holdum
  • 12,993
  • 6
  • 45
  • 74
  • 1
    Possible duplicates: http://stackoverflow.com/questions/129989/how-to-call-java-code-from-c http://stackoverflow.com/questions/246096/how-can-i-integrate-java-with-net http://stackoverflow.com/questions/152967/can-you-use-java-libraries-in-a-vb-net-program http://stackoverflow.com/questions/191622/c-java-interoperation – Jørn Schou-Rode Mar 17 '10 at 12:51

4 Answers4

8

Sorry, you cannot call java code / classes Directly from C# code.

One way of doing this is to wrap up your java classes in a java Web Service and call classes indirectly through that web service interface in your C# code.

Another way is using javareg.exe which exposes java classes as COM. You can find it at following location:

C:\Program Files\Microsoft VisualStudio\VIntDev98\bin\javareg.exe

Following posts might help as well

Asad
  • 21,468
  • 17
  • 69
  • 94
5

The simplest approach would probably be to publish the functionality of your java library as web services and add a web-reference from your asp.net application.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
2

Java isn't meant to be embedded in another program, so you need a bridge. The most simple solution is to use a socket: Create a Java process which listens for commands on a socket. In the C#, send the commands to the socket and read the answers.

The main problem here is serialization but if you use XML, it's not such a big pain anymore. Try the built-in XML serialization (see this article) or custom frameworks like XStream or Simple.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    "Java isn't meant to be embedded in another program" Except web browsers (Java Applets). – Powerlord Mar 17 '10 at 13:20
  • @Powerlord: Nice idea. So you just have to emulate the Netscape browser API in your application ... but then, I'm not sure whether any normal developer can handle the security setup, avoiding to have to actually open the applet on the screen somewhere, etc. – Aaron Digulla Mar 17 '10 at 15:16
  • Digullla: I wasn't suggesting that they do, I was just noting that there is an exception. – Powerlord Mar 17 '10 at 17:02
0

It is certainly possible to wrap Java in a .dll, and has been a part of the core Java platform for over 10 years. JNI (Java Native Interface) has an interface for embedding a JVM in your code, meaning you can run Java classes using C-style linking. Note that this will require that you write a simple C wrapper, there are samples within: http://java.sun.com/docs/books/jni/html/invoke.html#11202

As some of these other posts suggest, sometimes it's desirable to be less tightly coupled, so you may want to consider using another design. One option would be a simple database, where the Java application regularly polls for requests from the C# code. If you want tighter coupling, for things like call-backs, you can look at distributed interfaces.

Tristan Reid
  • 5,844
  • 2
  • 26
  • 31
  • I am stuck at call-backs. What do you mean by distributed interfaces? – Thamarai T Oct 08 '21 at 12:18
  • 1
    If you're creating a tightly linked local solution, JNI is probably still worth considering (it does support call-backs), but if you want less tightly coupled than a local solution, and more tightly coupled than a polling-based solution, you can use distributed interfaces like SOAP. JAX-WS for example supports callbacks. – Tristan Reid Oct 08 '21 at 19:29
  • I got redirected here because of JNI. Kindly refer this [link](https://stackoverflow.com/questions/47738401/interface-callback-in-xamarin/47738531#comment122702646_47738531) – Thamarai T Oct 09 '21 at 07:54
  • I am stucking at somewhere to get solution for [this](https://stackoverflow.com/questions/48420680/how-to-implement-ipay88-payment-gateway-sdk) This redirects me to JNI and wrappers. – Thamarai T Oct 09 '21 at 07:57