integrating java with C#.net - c#

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

Related

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#.

can use a library created in vb.net or c# in java?

I have written some classes in C# and compiled it. Now I have the library file for these classes. Can I use the same dll with Java?
I do not want to write it in Java once again because I am writing the same program in different languages (trying to do so).
So I am making the classes to a library file and want to use it in all the programing languages.
Java normally uses a completely different runtime (inside the JVM). There are tools intending to help bridge the gap (IKVM leaps to mind), but in general: no, you can't do this. Of course, you can use IPC etc to talk between them, but they are not best friends.
One area you can look into is JNI (Java Native Interface), see http://en.wikipedia.org/wiki/Java_Native_Interface as a starting point. There are possibly easier to use bridges for .NET to Java, ah here we go, see Calling C# code from Java?
You need to searialize your code into byte, xml or json. so you can use your codes in another language

Easiest way to call Java from 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.

C# GUI Frontend

Is is possible to make a GUI in C# but make the actually program in C or C++.
Like say I want to make a chat application. I want the interface to be in C#. But I want to make all the actual code in C.
Is this possible?
I found http://www.google.com/search?hl=en&q=P%2FInvoke&btnG=Google+Search&aq=f&oq=
does anyone have any better kind of information?
Absoluely! A c# winform can call into managed or unmaged C or C++ dll's. See P/Invoke for more information.
Edit: Turns out there's a good thread on P/Invoke on the front page. :)
You already have some answers here giving you the affirmative.
One suggestion tho: Premature optimization is the root of all evil (or something like that).
Unless you are creating a science project (doing something just to see if it can be done), just write the entire thing in C#.
Once complete, if you find you have parts of the project that are not performing well enough, rewrite those parts.
Basically, the part of the application that are slow are rarely where you thing they will be.
Incrementing on JP's totally correct previous answer, you can also expose your C++ code through COM interfaces and then consume them in C# via COM/Interop. Good luck, though.
Not only is it possible, it might very well be what you should do. There's no reason to rewrite all of your existing code for .NET.
In short, there are three ways to use existing (C/C++) code from C#: for easy access to a few simple APIs, P/Invoke works well. C++/CLI will give you the most flexibility. You third option is to use COM.
Everything you can do in C can be done in managed code. Why do you want to build one component in C# and the other one in C? Are you worried about sockets?
But to your question, you can absolutely do it as JP mentioned above.
Depending on how you want to distribute your application, you should consider the deployment requirements. If you have C++ modules, you will have a dependency on the C++ runtime libraries, in addition to the .Net framework. This is a non-issue if you are merely P/Invoking the Win32 API.
This may or may not matter to you, but it mattered to me - I got rid of the C++ layer in my app, and resorted to calling Win32 directly.

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