{"id":233,"date":"2014-03-02T02:44:14","date_gmt":"2014-03-02T10:44:14","guid":{"rendered":"http:\/\/blog.danielparnell.com\/?p=233"},"modified":"2014-03-02T02:44:14","modified_gmt":"2014-03-02T10:44:14","slug":"objective-c-to-java-and-back-again-via-jni-and-unix-pipes","status":"publish","type":"post","link":"https:\/\/blog.danielparnell.com\/?p=233","title":{"rendered":"Objective-C to Java and back again via JNI and Unix pipes"},"content":{"rendered":"<p>I&#8217;ve been working on a piece of software for some time now that is almost ready for a public release. \u00c2\u00a0For part of it I needed to connect to various databases, some of which are only accessible via JDBC. \u00c2\u00a0My problem is that my app is written in Objective-C. \u00c2\u00a0For the first version of my app this wasn&#8217;t a problem as Apple was promoting their brand new Objective-C to Java bridge and everything worked really nicely. \u00c2\u00a0That was until a couple of years ago when Apple decided to deprecate the Java bridge. \u00c2\u00a0I rewrote that code to use JNI and all was good. \u00c2\u00a0Then Apple brought out the Mac App store but announced that it would not accept any apps that link directly to the JVM. \u00c2\u00a0This causes a problem for me as without the JDBC drivers the usefulness of my app is greatly reduced.<\/p>\n<p>I looked into various ways of embedding the JVM into my app, but the problem with doing that was that it increased my app from less than 10Mb to around 100Mb. \u00c2\u00a0I wasn&#8217;t too happy about that to say the least, so I looked into providing a way to make my app optionally use the system JVM allowing the Java support to be turned off. \u00c2\u00a0This worked pretty well unless the machine didn&#8217;t have a JVM installed in which case my app would refuse to load due to linkage failures. \u00c2\u00a0So, I had the bright idea to use Apples &#8220;new&#8221; XPC feature. \u00c2\u00a0I reorganised my JNI code to allow it to be called from an XPC process and found that the security settings meant that the JVM could not access any of the JDBC driver files in my app bundle. I got around that by using a folder shared with a process group. \u00c2\u00a0This allows the JDBC drivers to be loaded, but then I found that for some reason the JNI code that had been running very nicely for a couple of years didn&#8217;t like running inside an XPC process and would fail randomly with strange errors. \u00c2\u00a0On top of that I found that the process of going from Java, through JNI to Objective-C, then from the XPC process over to the main app was very slow and was especially noticeable with some of my larger data sets.<\/p>\n<p>In desperation I decided to abandon the JNI code altogether and see if I could communicate from the main app to the java code in some other way. \u00c2\u00a0In the end I created a Java JDBC connector that communicates with the main app via a pair of Unix pipes. \u00c2\u00a0I have found this to amazingly fast and extremely reliable. \u00c2\u00a0Instead of the data going through several transformations from Java, through to JNI and then to Objective-C and so on it now goes directly from the Java code in a special serialised format directly into the Objective-C code via the pipes. \u00c2\u00a0It works really well \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a piece of software for some time now that is almost ready for a public release. \u00c2\u00a0For part of it I needed to connect to various databases, some of which are only accessible via JDBC. \u00c2\u00a0My problem is that my app is written in Objective-C. \u00c2\u00a0For the first version of my &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.danielparnell.com\/?p=233\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Objective-C to Java and back again via JNI and Unix pipes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,45,3,2],"tags":[],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-java","category-mac-os-x","category-programming","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p561S3-3L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=233"}],"version-history":[{"count":2,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}