开发者

Interacting with java code from C#

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开发者_如何学Python 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.


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

  • Calling Java Classes Directly from .NET (uses runtime bridge)
  • Calling Java from Microsoft.NET


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜