Easiest way to call Java from C#? - c#

We have a major body of code in Java that runs on the desktop that we want to reuse with a MS.NET user interface (desktop rather than web). Any do's or don'ts would be very welcome.

You may want to take a look at IKVM.
It is an implementation of the Java Virtual Machine on top of the .NET Framework. There are some parts dealing specifically with interoperability of the .NET and Java worlds so you can (more or less) seamlessly use one from the other.

I suggest creating web services for existing java code to act as a wrapper and call them from .net app. (desktop or web)

If you have the $$$$ you can try mainsoft; it converts your java code in C#, or vice versa, depending on your whim.
Eric Sink has a post about this.

Another possible solution could in the the form of j-interop
I've used it successfully for calling from Java -> DCOM and it claims to be able to do bi-directional stuff so in theory it should be possible to register a Java process as a DCOM server.
You will need to do a bit of work on the java side as it won't be able to just make it work automatically.

I agree with the webservices method suggested by "2009MIPS". It offers the cleanest and most "debuggable" way to do it.

Related

What's the best way to call c# code in c code on linux

I am doing a project and have a C# library but I need to call it in C code on Linux. What's the most efficient way to do this? Performance is the first consideration.
Of course, I can make a C# service and use TCP to talk. But I wonder if it's the best way...
Thank you!
You can also use CoreRT to create native libraries from C# code and call them from C. You can find an example here
Edit: CoreRT has moved to runtimelab feature NativeAOT. Updated link.
I think the most standard solution is to have your C# service export a "web services" remote API, which the C code can then access via the standard Linux web services packages. In effect these support a form of RPC, but with web pages encoding the request and also the reply. The advantage of using web services is generality; your solution will port to any platform and will work from any language, not just C.
Another way to go is to use one of the new open-source packages from Google, Facebook, Twitter, etc. A bunch of the cloud vendors are open-sourcing their RPC infrastructures because many customers pose your exact question and need these solutions. Those will definitely be faster and more powerful in other ways too: they include powerful performance visualization tools and debuggers of various kinds. Microsoft has a remote method invocation technology too. So these are an option, perhaps less general but definitely faster. So you then have to ask how important speed is, for this particular path...
The bottom line then is that there are maybe five packages you could use. No idea which is winning or best!

Return data from a Java process back to calling C#. Possible?

I'll explain breifly my situation and hopefully you will be able to advise if what im wanting to do is possible.
I have an existing java application that I am wanting to split into modules. To handle and control these modules Im going to write a module manager in C#.net. Due to the size of the existing program the bulk of the existing modules are not going to be rewritten in .net yet and remain as java modules.
Is it possible to call a java "module", pass it parameters and have the java module return a value ( other than an int )?
I apologise for not knowing much about this area.
Kind Regards
Ash
Hmm... maybe some kind of MessageQueues like MSMQ, Apache ActiveMQ or IBM WebsphereMQ can solve your problem.
On the queues you can store and receive XML-Messages with all the Information you need.
Some information about this can be found here:
http://msdn.microsoft.com/en-us/library/ms973816.aspx
Another approch can be to work with console output .. but IMHO this is not a good solution.
I would instantiate the Java as a separate service and call it using (say) web services, Hessian etc.
Alternatively, have you looked at jni4net ?
If everything's in Java, then why the effort in moving everything to C#? From what you've said it'd make much more sense to write the module manager in Java and just keep the codebase all in one language (unless of course I'm missing something, in which case ignore!)
If you really need to do this then I'd say a web service is the nicest way to go, there's other hacks and various tools around that you could use, but a web service would completely abstract the language away and makes things much easier to consume.
I haven't tried this either but hopefully reading this thread helps you... :)
Java - C# interop
You can expose your Java module as a soap web-service and consume it from C#.
Here you can read about Axis one of the Java Soap engines and quick tutorial how to create and call it from C#.

integrating java with C#.net

Is following thing possible?if yes how?
I have some core functionality already implemented in Java. Now I want to create a GUI for it.
I am finding it difficult to make a GUI in Java using Swing. I can build a GUI easily in C#.net.
So I was thinking if it was possible to create a library in Java that I can access from my C# code.
Shorty: my java code will provide some API and then my C# program must be able to call those APIs.
How do I do this?
Assuming you have properly structured your Java code, creating a Web Service bridge between the two could be the way to go. For example you could expose the Java API through a SOAP and simply make the C# UI depend on that.
The great side in this solution of course is that if you do it this way, you can replace the UI completely at any point you want to, downside is that you would have to run the Java part separately in a context such a servlet container which may be quite heavyweight for your needs.
Depends on what your java library does and if it uses many third party components, but thanks to IKVM you could compile a jar file into a .NET assembly. I've used this approach to incorporate the xhtmlrenderer java library into a C# application with success.
Yes, this is possible.
Have a look at JNBrige: http://www.jnbridge.com/
But, to be honest, I think it would be better to rewrite your APIs in .Net. Makes it easier in the end imho. I prefer working in one environment and not having to maintain things at different places.
have you considered using webservices? You could also execute a java program using something like:
private void calljava(object ob,EventArgs arg)
{
Process.Start("j.bat");
}
There is also JNBridge
Just a few ideas....

Interaction between Java and C#

Is it possible to write a user interface in Java for an application written in C#?
I am working on a user interface of a project that is written in C#, but I have no experience with C# and I am an avid Java user. Is it possible to build the user interface in Java using Java's Swing and AWT libraries that operates an application primarily written in C#.
If this sounds like a really stupid question, I apologize in advance.
You might be able to leverage some of the interoperability features that are integrated into Mono 2.0
http://www.mono-project.com/Main_Page
JNBridge is another possible interoperability solution:
http://www.jnbridge.com/
However, a more optimal approach might be to expose your .NET code as Services - and then access them from the Java client (or through a light-weight ESB).
Of course, time, budget, resources are constraints you'll have to consider.
In addition to http://www.jnbridge.com (proprietary)
you can try http://www.janetdev.org, - open source implementation of a Java 5 SE JDK environment for the .NET platform. Currently it supports .Net 3.5 only (not Mono).
We did this recently and went the route of using a low level socket connection, but pushing xml through it. C# was the server side, and we used the Microsoft 'xsd' tool to generate the XSD schema for the objects and then used JAXB on the java side to generate java code to parse and hold the same objects.
As Barry mentions most of the work/problems was around the socket connection - but that depends on how comfortable you are with that.
Also, for a solution that cross-compiles your java to run in the CLR: http://www.ikvm.net/
I am author of jni4net, open source interprocess 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.

Calling C# code from Java?

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.
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/
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.
If it's short, I think you're better off re-writing the code in java. Downloading one 50Mb runtime is bad enough.
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.
We used JNBridge for this, and it worked great. It handles Java->.NET and vice versa, all in-proc.
If you do not want to rewrite hadle it as an Inter-process communication and choose one of following:
Named pipes
Sockets
SOAP
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.
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.
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.
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

Categories

Resources