Skip to content
Advertisement

Call Java function using .Net(C#)

is it possible to invoke function which is written in Java using WCF or any class application written in C# .net

Can it be possible by using webOrb.. i can’t find enough information about Java to .Net remoting..

Advertisement

Answer

If you want to communicate between C# and Java you have a couple of options.

The cleanest: Build a service.

This assumes you have access to the source code of both your C# component and your Java component. In the case that you want to call a method within Java, you can build a service that allows a connection from your C# client, to your Java service, and the service then executes the desired functionality, and returns a value back to the C# client. Some easy ways to do this is by building a RESTful service or using Thrift. I recommend you choose a solution similar to this one.

The most complex: Corba

Corba is a standard defined to communicate amongst different computer languages. Most mature languages have support for it, but it is a bit unusual, and the use of it has declined in favor of building service. This also assumes access to both source codes. You’d have to independently look for the information regarding how to use Corba on both Java and C#. I would really advice against this.

The dirtiest but quickest: Execute as process and parse output

I really do NOT recommend you to do it this way unless you really have no choice. This would entail executing a Java program from within C#. This is only a good choice when you have no other option, because all you have is an executable. If that were the case, you can use the Process class to execute the external program, sending it parameters, and then reading the output. See the example mentioned here: How do I start a process from C#?

This has many downsides though, as you’ll have to think of every exceptional cause, determine the output for those cases, and then determine how to parse that output. If the program has any level of complexity, before you know it, you’ll end up with hard to maintain code.

Conclusion: Build a Service

That’s probably your best bet. Build a service that exposes an API that the C# client can call on.

Advertisement