Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Java function/class from Delphi

Tags:

java

delphi

In delphi, I am trying to call a function from an external Java program. Is there any way to do it ?

like image 203
se-work Avatar asked Apr 15 '26 22:04

se-work


2 Answers

The standard process to call native code is via JNI. A search on JNI and Delphi will reveal multiple pages that detail how this is done, like this and this

What is more desirable (setting up some out of process server (like Peter already detailed, so I skipped that) or using JNI to call a library depends on how often (and how realtime) you need this to be, and on allowable installation/configuration complexity

like image 141
Marco van de Voort Avatar answered Apr 17 '26 10:04

Marco van de Voort


If it is a running Java application you will need to expose access to that function. There are a myriad of solutions possible.

If it is only 1 function or very limited functionality, then listening on the humble socket or named pipe is a solution which is currently undervalued and kind of forgotten.

On the next step of integration I would look at asynchronous message passing. It is easy to embed an activemq server or similar or start it in a separate process. This has a number of advantages like that the request are easily synchronized in the Java process by simply using one listening thread, that the behavior is well defined when the Java program is not available or the Delphi one. It is very easy to manage and you get the instrumentation for free.

An embedded Jetty webserver is an easy, reliable solution and implement a servlet to do your bidding. Again a lot of the complexity is now handled by using ubiquitous and standard protocols.

Then there are the synchronous RPC methods like COM, Corba, SOAP which I personally find much too complex, error-prone and maintenance unfriendly to use for ad-hoc communication between processes. If you want to build a complete infrastructure of stuff talking to each other it might be worth it, but not to get 2 programs talking.

like image 39
Peter Tillemans Avatar answered Apr 17 '26 11:04

Peter Tillemans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!