views:

154

answers:

3

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.

+3  A: 

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.

klausbyskov
+5  A: 

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 Butt
+2  A: 

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
"Java isn't meant to be embedded in another program" Except web browsers (Java Applets).
R. Bemrose
@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
@Aaron Digullla: I wasn't suggesting that they do, I was just noting that there is an exception.
R. Bemrose