{"id":10,"date":"2011-08-27T21:03:43","date_gmt":"2011-08-28T04:03:43","guid":{"rendered":"http:\/\/flenniken.net\/blog\/?p=10"},"modified":"2011-08-27T21:03:43","modified_gmt":"2011-08-28T04:03:43","slug":"java-calling-native-code","status":"publish","type":"post","link":"https:\/\/flenniken.net\/blog\/java-calling-native-code\/","title":{"rendered":"Java Calling Native Code"},"content":{"rendered":"<h3>JNI vers Executing Processes<\/h3>\n<p>In a Java environment you sometimes need to call native code written in C++ or C.\u00a0 You can do this in two ways, using the Java Native Interface JNI\/JNA, or by executing native stand alone applications.<\/p>\n<p>When you use JNI, the native code runs in the same process as the calling Java code.<\/p>\n<p>When you execute a stand alone application, the application runs as a separate process.<\/p>\n<p>The advantages of using JNI are:<\/p>\n<ul>\n<li>Its potentially faster since there is no process switch.<\/li>\n<li>Error handling is easy. You can use exception handling in the JNI code to return Java exceptions.<\/li>\n<li>It&#8217;s easy to call JNI code since you call the JNI code just like a normal Java method.<\/li>\n<li>You can share memory between the JNI code and the Java code (potentially faster processing).<\/li>\n<\/ul>\n<p>The advantages to executing system processes are:<\/p>\n<ul>\n<li>Crash protection, the code is isolated through a process boundary.\u00a0 If the process crashes, you can catch it in Java and continue.<\/li>\n<li>You can run the code outside the Java environment easily and develop it independent of Java.<\/li>\n<\/ul>\n<p>The disadvantages of using JNI are:<\/p>\n<ul>\n<li>A crash in JNI code will bring down the Java VM.<\/li>\n<li>It&#8217;s hard to setup, you must create binding code between Java and JNI.\u00a0 (Java Native Access (JNA) makes this easier.)<\/li>\n<li>Hard to run JNI code outside of Java.<\/li>\n<\/ul>\n<p>The disadvantages of executing processes are:<\/p>\n<ul>\n<li>Starting a process is hard, you must write platform dependent code in Java to start the process.<\/li>\n<li>It&#8217;s is clumsy to call, you must pass command line arguments, environment variables. and a starting directory.<\/li>\n<li>Loading of shared libraries is a hard platform dependent problem.<\/li>\n<li>Error handling is convoluted, you get one int return value, or you must parse standard error or standard out.<\/li>\n<li>You cannot share memory between the Java code and the JNI code.<\/li>\n<\/ul>\n<p>You may argue that you can protect the JNI code using C++ exception handling but this is not the case.\u00a0 Below is an example crasher that is not caught.<\/p>\n<pre>int crasher()\r\n{\r\n    try\r\n    {\r\n        int *ptr = 0;\r\n        *ptr = 1234;\r\n    }\r\n    catch (...)\r\n    {\r\n        \/\/ never gets here\r\n        printf(\"caught exception\");\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>Why doesn&#8217;t this work? If this was an option, crashing native code could be protected. It seems like an issue with the Java VM.\u00a0 Here is a note I found under the <a href=\"http:\/\/jna.java.net\/javadoc\/com\/sun\/jna\/Native.html#setProtected%28boolean%29\">JNA documentation<\/a>:<\/p>\n<blockquote><p>&#8220;Set whether native memory accesses are protected from invalid accesses. This should only be set true when testing or debugging, and should not be considered reliable or robust for applications where JNA native calls are occurring on multiple threads. Protected mode will be automatically set if the system property jna.protected has a value of &#8220;true&#8221; when the JNA library is first loaded.<br \/>\nIf not supported by the underlying platform, this setting will have no effect.&#8221;<\/p><\/blockquote>\n<p>For the project I was working on the crash protection issue overrode all others.\u00a0 With our limited testing resources and the complexity of our JNI code, we did not want to take the risk of a crashing bug in the native code bring Java down.\u00a0 So we switched from JNI to native command line applications.<\/p>\n<p>If you have the resources to test and make your native code stable, JNI is the better approach.\u00a0 Calling system DLLs with JNA is the best approach for calling stable system features not already in the Java API.\u00a0 Otherwise consider writing your native code as stand alone command line applications and executing the code in a separate process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JNI vers Executing Processes In a Java environment you sometimes need to call native code written in C++ or C.\u00a0 You can do this in two ways, using the Java Native Interface JNI\/JNA, or by executing native stand alone applications. &hellip; <a href=\"https:\/\/flenniken.net\/blog\/java-calling-native-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[5,6],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-jni"],"_links":{"self":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":22,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}