views:

134

answers:

8

What can we do to integrate code written in a language with code written in any other language? Which techniques are more/less known? I know that some/most languages can be compiled to Java bytecode, but what do we do about the rest ?

+3  A: 

It depends on the level of integration you want.

  • Do you need the code to share data? Use a platform-neutral data format, such as JSON, XML, Protocol Buffers, Thrift etc.
  • Do you need to be able to ask code written in one language to perform some task for code in the other? Use a web service or similar inter-process communication layer.
  • Do you need to be able to call the code within a single process? The answer at that point will entirely depend on which languages you're talking about.
Jon Skeet
HTTP and web services -- work great for integration.
S.Lott
Leave it to the Google guy to mention "Protocol Buffers" :-)
WaldenL
I deliberately included 3 other formats at the same time though :)
Jon Skeet
+1  A: 

The two I see most often are SWIG and Thrift. The main difference is (IIRC) Thrift opens up a port and puts a server there to marshal the data between the different languages, whereas SWIG builds library interface files and uses those to call the specified methods.

Chris Bunch
+5  A: 

You mention the "compile to Java" approach, and there's also the "use a .NET language" approach, so let's look at other cases. There are a number of ways you can interoperate, and it depends on what you're trying to accomplish, it's a case by case situation. Things that come to mind are

  • Web Services (SOAP or REST)
  • A text (or other) file in the file system
  • Use of a database to relay state or other data
  • A messaging environment like MSMQ or MQSeries
  • TCP sockets or UDP messages
  • Mailslots and named pipes
WaldenL
+1  A: 

I think there are a few possible relationships among programs in different langauges...

There's shares a runtime (e.g. C# and Visual Basic) and compiled into same application/process...

There's one invokes the other (e.g. perl script that invokes a C program)...

There's talks to each other via IPC on the box, or over the network (e.g. pipes and web services)...

Brian
+2  A: 

Direct invocations:

  • Direct calls (if the compilers understand each other's call stack)
  • Remote Procedure Call (early 90's)
  • CORBA (late 90's)
  • Remote Method Invocation (Java, with RMI stack/library in target environment)
  • .Net Remoting

Less tightly integrated:

  • Web services/SOAP
  • REST
cdonner
+1  A: 

Unfortunately your question is rather vague.

There are ways to use different languages in the same process usually by embedding a VM or an interpreter into the executable. If you need to communicate over process boundaries there again are several possibilities many of them have been already mentioned by other answers.

I would suggest you refine your question to get more helpful answers.

lothar
+1  A: 

On the Web, cookies can be set to pass variables between ASP/PHP/JavaScript. On a previous project I worked on, we used this to create a PHP file for downloading PDFs without revealing their location on the file system from an ASP application.

Scott
+1  A: 

Almost every language that pretends some kind of system's development use is capable of linking against external routines with either a standard OS interface, or a C function interface. That is what I tend to use.

T.E.D.