Is it possible for Servoy to call methods within a shared library written in C ?
Hi Mark,
Technically possible using the JNI - the Java Native Interface.
The JNI is not trivial and requires a considerable effort to learn. Some things to watch out for:
- subtle errors in the use of JNI can destabilize the entire JVM in ways that are very difficult to reproduce and debug;
- only applications and signed applets can invoke the JNI;
- an application that relies on JNI loses the platform portability Java offers (a workaround is to write a separate implementation of the JNI code for each platform and have Java detect the Operating System and load the correct one at runtime);
- there is no garbage collection for the JNI side (JNI code must do explicit deallocation);
- error checking is a MUST or it has the potential to crash the JNI side and the JVM.
Hope this helps.
While I agree that there are many disadvantages to using a C-library there are a few advantages that may make the payback case more even such as:
-
IF a large legacy effort was involved and the product is still being maintained then there is no point to porting to Java then invoking via Javascript in Servoy.
-
Sometimes (as was my case recently) one is mandated to use a third party library written in C/C++ so there is no choice since no c-source code is available (usually). So the effort to create the JNI , though perhaps large and risky for a newbie, may be able to payback over the long run.
3.Sometimes there is an elegant C/C++ library for specialized or niche applications or real-time control that is almost impossible to port to Java.
I just completed a JNI for some 150 complex classes written in C++/Fortran for scientific applications in simulation that are economically not feasible to port to Java. And yes I did have to check for errors and exceptions constantly in order not to cause instability. However it was nice to be able to create a Java instance at the JNI/C++ level whenever I needed to have a Java object to encapsulate data.
So the possibilities are good: Use Servoy for the main application; use Javascript for calls to Java object/methods that call JNI for C++ library (or other dlls) to do some detailed work and pass all the results back up the line to Servoy.
Now this is a powerful multi-language platform!
My best advice is to do a really good trade study (feasibility study) to see if the effort and the payback and the maintenance are suitable.
Tom
I agree that there are many times when using a C-lib might be valuable or required, but would tend to think that it might be better (more maintainable, flexible, reliable, portable, etc.) to create an HTTP-REST service with a C-friendly language like Ruby/Python/Perl that wraps the necessary library functions and make the calls to it from Servoy that way.
My $0.02.
greg.
or you can use your sql database server to call the c library, for instance in oracle you can call externall c library dll’s using pl/sql. See this article: http://www.dbasupport.com/oracle/ora9i/ … ures.shtml ‘Managing Processes for External Procedures’.
may be this is also possible fom sql server or other brands of sql databases.
But again: I quote 'You should call an External Routine only when the computational benefits outweigh the cost. ’
may be using a webservice(s) is the better alternative.
regards.
Jan Willem
Fortuitous that this post should come up – I am currently looking at developing a Servoy solution that eventually connects to the SQL database used by another application. The problem is that this application uses its own C API to modify the SQL database – no direct SQL queries are made by the application. It is also against the licence agreement to directly alter the underlying database unless you use the API. The reason is to ensure the integrity of the data, as modifying one table often has considerable ramifications in other tables across the database. This is fine if I were using C…
The API is reasonably well documented. Could the Java native Interface be used to call (and pass parameters from Servoy) to the application’s API functions?
If it can, how hard is it to learn (including error checking etc) and where would you start?
It would be quite a coup if I could pull it off.
Start with:Oracle Java Technologies | Oracle
This is not a trivial task, nor for the faint-hearted.
Then google for more tutorials until your are familiar enough to try some on your own. Do not forget that you will also need a C/C++ compiler/linker because the JNI is in C++. If that has not scared you away, then you are well on your way to a challenge! I use the MinGW as my C/C++ compiler but others will do. If this does scare you then I suggest you subcontract to someone who can provide the JNI for you Java code and also teach you how to use the Java in the Servoy Javascript!
Is the C API in the form of a dll? IS the C API well documented so that the types, length and return value/type well documented and understood?
Off the top of my head if you have done C/C++ and Java before then the learning curve is 4 weeks or so for the basics. For example if all the arguments of a C function are primitive types then it is easy. However if there are arrays involved then it is more elaborate (read more error prone to get it correct). Strings are the first difficulty to overcome I would imagine but once mastered, then there is so much repetition.
Cheers
Tom
You can look also into articles using the jacob interfacing (JAva COM Bridge), which can be used to call Word/Excel as COM abjects from java or groovy programs/scripts. It is opensource and may be this gives you also enough background to get the results you wanted.
An overview article is http://dn.codegear.com/article/33305
I even played around it with using groovy to connect to an Oracle database via the Oracle for OLE Objects (O4OO) instead of using JDBC on Windows platform.
But in a production site we use JACOB to integrate a java based workflowsystem with MS-Word to generate letters (Office integration).
regards,
Jan Willem Teunisse
Fortunately, the C functions all use primitives as parameters (i.e. floating point is as ferocious as it gets) so our data conversion issues shouldn’t be a concern at all.
I have crackerjack Java, C and C++ programmers at my disposal. Work begins on this in the next week or so - I’ll try to keep you posted.