{"id":147,"date":"2007-01-05T12:02:32","date_gmt":"2007-01-05T17:02:32","guid":{"rendered":"http:\/\/www.graphics-muse.org\/wp\/?page_id=147"},"modified":"2009-04-01T10:40:14","modified_gmt":"2009-04-01T17:40:14","slug":"building-jni-based-java-applications-under-linux-and-cygwin","status":"publish","type":"page","link":"https:\/\/www.graphics-muse.org\/wp\/?page_id=147","title":{"rendered":"Building JNI-based Java Applications under Linux and Cygwin"},"content":{"rendered":"<p>JNI is the <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/guide\/jni\/\">Java Native Interface<\/a>.\u00a0 It&#8217;s a mechanism in Java for calling C programs (perhaps other languages as well) that provide native functionality.\u00a0 By native I mean platform specific.\u00a0 This seems like heresy to Java purists, but in practice I can&#8217;t imagine making use of real cross platform capabilities without making use of platform specific features.\u00a0 If you&#8217;ve ever tried to work with process management or do anything complex with filesystems under Java, you&#8217;ll understand what I mean.<\/p>\n<h3>Important Links<\/h3>\n<ul>\n<li><a href=\"http:\/\/rangiroa.essi.fr\/cours\/turorial%20java\/native1.1\/ \">Java Native Interface<\/a> &#8211; This is probably the best overview of JNI<\/li>\n<li><a href=\"http:\/\/www.acm.org\/crossroads\/xrds4-2\/jni.html\">Using The Java Native Interface<\/a><\/li>\n<li><a href=\"http:\/\/www.javaworld.com\/javaworld\/jw-10-1999\/jw-10-jni.html?page=1\">Enhance your Java application with Java Native Interface (JNI)<\/a><\/li>\n<li><a href=\"http:\/\/www.geocities.com\/kongwenyu\/jni.html\">A Brief Introduction to Java Native Interface<\/a><\/li>\n<li><a href=\"http:\/\/ringlord.com\/publications\/jni-howto\/\">The JNI How-TO by Udo Shuermann<\/a><\/li>\n<li><a href=\"http:\/\/rangiroa.essi.fr\/cours\/turorial%20java\/native1.1\/invoking\/invo.html\">Invoking the Java Virtual Machine<\/a> &#8211; re: for how to build a C binding to a Java library.<\/li>\n<li><a href=\"http:\/\/www.cygwin.com\/ml\/cygwin\/2001-05\/msg01226.html\">DLL for Java Native Interface<\/a> &#8211; post to Cygwin mailing list<\/li>\n<li><a href=\"http:\/\/www.inonit.com\/cygwin\/jni\/helloWorld\/\">Using Cygwin To Develop Windows JNI Methods<\/a><\/li>\n<\/ul>\n<hr size=\"2\" \/>\n<h3><span style=\"color: #993300; font-size: medium;\">Building JNI applications under Cygwin<\/span><\/h3>\n<h3><\/h3>\n<h3><span style=\"color: #000080;\">What is Cygwin?<\/span><\/h3>\n<p><a class=\"urllink\" rel=\"nofollow\" href=\"http:\/\/www.cygwin.com\/\">Cygwin<\/a> is a complete Unix-styled environment designed for use under various Windows operating systems. Cygwin provides all the basic GNU-based utilities plus a complete X environment. This allows you to create a tolerable environment under which you can do development work for a completely untolerable operating system (re: Windows).<\/p>\n<p class=\"vspace\">By using Cygwin you can simulate your Linux desktop in order to do development. Any binary built with Cygwin&#8217;s gcc has all (or nearly all) the same functionality you&#8217;d find in any POSIX compliant Unix system. The Cygwin dll&#8217;s provide any such functionality missing from the Windows libraries.<\/p>\n<p class=\"vspace\">The drawback to using Cygwin&#8217;s gcc compiler, however, is that any binary created with it requires the Cygwin libraries be installed too. This is not something you want to force upon your non-Unix literate customers.\u00a0 They want native Windows applications.<\/p>\n<p class=\"vspace\">Despite this drawback, you can still use Cygwin to do develpment work. You just have to take care to build your software in a way that doesn&#8217;t depend on Cygwin. As it turns out, that&#8217;s not very hard to do.<\/p>\n<h3><span style=\"color: #000080;\">Differences between Unix and Cygwin<\/span><\/h3>\n<p>There are few differences between your Unix\/Linux environment and Cygwin. From a user perspective, you can essentially run all the tools you&#8217;re used to, even under the same desktop environment (if you&#8217;re up to the challenge of building GNOME or KDE under Cygwin\/X &#8211; I fell back to an easier but just as feature rich environment: XFCE).<\/p>\n<h3><span style=\"color: #000080;\">Differences between Windows and Cygwin<\/span><\/h3>\n<p>Path references are different between Windows and Cygwin. Windows uses the drive letter prefix (eg C:\\) while Cygwin uses the root directory instead (eg \/cygwin). This is an important subject because Java and Ant on Windows are Win32 binaries and don&#8217;t know about Cygwin&#8217;s Unix paths.<\/p>\n<p class=\"vspace\" style=\"border: 1px solid black; margin: 20px 60px; padding: 5px 25px; background-color: #cccccc;\">When setting up the build environment you have to use the Windows path definitions to find your Java and JBOSS installation directories.<\/p>\n<p class=\"vspace\">You can access Cygwin directories using Windows paths by first changing into the Cygwin installation directories:<\/p>\n<p class=\"vspace\" style=\"border: 1px solid black; padding: 5px 20px; background-color: #ffffaa; color: #aa3333; font-weight: bold;\">cd C:\\cygwin<\/p>\n<p class=\"vspace\">Under here you will find all the traditional directories like <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">usr<\/span>, <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">lib<\/span>, <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">home<\/span> and <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">etc<\/span>.<\/p>\n<p class=\"vspace\">To get to the Windows directories from under Cygwin&#8217;s Unix-style paths, you change to the special <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">\/cygrdrive<\/span> directory:<\/p>\n<p class=\"vspace\" style=\"border: 1px solid black; padding: 5px 20px; background-color: #ffffaa; color: #aa3333; font-weight: bold;\">cd \/cygdrive\/c\/<\/p>\n<p class=\"vspace\">The letter after <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">\/cygdrive\/<\/span> is the drive designation.<\/p>\n<h3><span style=\"color: #000080;\">What is MinGW?<\/span><\/h3>\n<p><a class=\"urllink\" rel=\"nofollow\" href=\"http:\/\/www.mingw.org\/\">MinGW<\/a> is a collection of headers and libraries that make building applications using Cygwin&#8217;s gcc capable of being run only with the Microsoft Win32 API. By using <span class=\"wikiword\">MinGW<\/span> you can remove the requirement for Cygwin&#8217;s libraries. However, it also means you&#8217;re limited to the Microsoft Win32 API, which means you code (or build system) will have lots of #ifdef&#8217;s to handle building under either operating system.<\/p>\n<h3><span style=\"color: #000080;\">How to install<\/span><\/h3>\n<p>Both Cygwin and <span class=\"wikiword\">MinGW<\/span> can be installed by using the graphical installer you can download and run from the <a class=\"urllink\" rel=\"nofollow\" href=\"http:\/\/www.cygwin.com\/\">Cygwin web site<\/a>.  You will probably need to find the <span class=\"wikiword\">MinGW<\/span> development tools and enabled them since they may not be installed by default.<\/p>\n<h3><span style=\"color: #000080;\">Setting up environment<\/span><\/h3>\n<p>In order to build Win32 API applications (or in our case, the Win32 API based JNI library) you have to tell Cygwin&#8217;s gcc to use <span class=\"wikiword\">MinGW<\/span>.  This is done simply by setting the <span style=\"color: #580115; font-weight: bold; font-style: italic;\">&#8211;mno-cygwin<\/span> command line option to gcc:<\/p>\n<p class=\"vspace\" style=\"border: 1px solid black; padding: 5px 20px; background-color: #ffffaa; color: #aa3333; font-weight: bold;\">gcc &#8211;mno-cygwin<\/p>\n<p class=\"vspace\">This option must be specified for both compiles and linking, so setting GCC=gcc &#8211;mno-cygwin is probably the easiest approach. We actually use a number of different options for compiling and linking so the JNI library&#8217;s Makefile creates a HEADERS and a LINKARGS variable that includes this option.<\/p>\n<h3><span style=\"color: #000080;\">Java mods for Cygwin Builds<\/span><\/h3>\n<p>Under Cygwin, the JNI (Java Native Interface) library we created called <em><span class=\"wikiword\">JNILibrary<\/span><\/em> doesn&#8217;t build because gcc doesn&#8217;t know about the type &#8220;__int64&#8221;. You&#8217;ll know you hit the problem if you see something like this:<\/p>\n<p><span style=\"color: #000000;\"> Building JNILibrary class and header&#8230;<br \/>\nIn file included from \/cygdrive\/c\/j2sdk1.4.2_12\/include\/jni.h:27,<br \/>\nfrom JNICrunch-common.h:25,<br \/>\nfrom JNICrunchHWInfo.c:31:<br \/>\n\/cygdrive\/c\/j2sdk1.4.2_12\/include\/win32\/jni_md.h:16: error: parse error before &#8220;jlong&#8221;<br \/>\n\/cygdrive\/c\/j2sdk1.4.2_12\/include\/win32\/jni_md.h:16: warning: data definition has no type or storage class<br \/>\n<\/span><\/p>\n<p class=\"vspace\">If you do hit this, then you need to edit \/cygdrive\/c\/j2sdk1.4.2_12\/include\/win32\/jni_md.h and change these lines:<\/p>\n<blockquote><p><span style=\"font-size: x-small;\"> typedef long jint;<br \/>\ntypedef __int64 jlong;<br \/>\ntypedef signed char jbyte;<\/span><\/p><\/blockquote>\n<p>to:<\/p>\n<blockquote><p><span style=\"font-size: x-small;\"> typedef long jint;<br \/>\n#ifdef __GNUC__<br \/>\ntypedef long long jlong;<br \/>\n#else<br \/>\ntypedef __int64 jlong;<br \/>\n#endif<br \/>\ntypedef signed char jbyte;<\/span><\/p><\/blockquote>\n<p>This assumes you installed the Java SDK under <span style=\"color: #0055aa; font-family: sans; font-size: 7pt; text-decoration: underline;\">C:\\j2sdk1.4.2_12<\/span><\/p>\n<h3>Important Links &#8211; Note:  if the MSDN links move, try searching for Windows API Reference<\/h3>\n<ul>\n<li><a class=\"urllink\" rel=\"nofollow\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa383749.aspx\">Win32 API Documentation<\/a><\/li>\n<li><a class=\"urllink\" rel=\"nofollow\" href=\"http:\/\/www.nersc.gov\/%7Escottc\/misc\/docs\/snort-2.1.1-RC1\/config_8h.html\">Snort config.h<\/a> which lists some macros for POSIX-&gt;Win32 API conversion (like bzero, bcopy, etc.).<\/li>\n<li><a class=\"urllink\" rel=\"nofollow\" href=\"http:\/\/www.cygwin.com\/ml\/cygwin\/2004-03\/msg00289.html\">Note about using &#8211;mno-cygwin<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>JNI is the Java Native Interface.\u00a0 It&#8217;s a mechanism in Java for calling C programs (perhaps other languages as well) that provide native functionality.\u00a0 By native I mean platform specific.\u00a0 This seems like heresy to Java purists, but in practice I can&#8217;t imagine making use of real cross platform capabilities without making use of platform [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":146,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-147","page","type-page","status-publish","czr-hentry"],"aioseo_notices":[],"jetpack_shortlink":"https:\/\/wp.me\/Pe9t8-2n","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/pages\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=147"}],"version-history":[{"count":1,"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/pages\/147\/revisions"}],"predecessor-version":[{"id":489,"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/pages\/147\/revisions\/489"}],"up":[{"embeddable":true,"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=\/wp\/v2\/pages\/146"}],"wp:attachment":[{"href":"https:\/\/www.graphics-muse.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}