Welcome!

Java Authors: Carmen Gonzalez, Jim Kaskade, Aater Suleman, Pat Romanski, Paige Leidig

Related Topics: Java

Java: Article

Calling Java From C

A framework for easier JNI

Though most Java developers think of the Java Native Interface (JNI) as a framework for developing native libraries that can be called from Java, relatively few know that JNI also supports communication in the reverse direction: it provides native programs written in C with the ability to call Java objects. However, the coding is thorny; logic that can be coded readily in a few lines of Java requires several times more lines of C, thanks to JNI's granular programming model and peculiar approaches to exception handling and garbage collection. This article explores the nature and typical use of the C-to-Java JNI interface and presents the design of a framework that eases the programming effort.

The JNI Architecture
As Figure 1 illustrates, JNI is actually a pair of APIs:

  • "JNI Proper" supports the manipulation of Java objects and classes, such as the ability to call object methods.
  • The Invocation API is a smaller C library that enables C programs to create and destroy a Java Virtual Machine (JVM).
A C-to-Java program (that is, a C program that uses Java) calls the Invocation API to create a JVM, and calls JNI Proper to use Java objects. As for the Java-to-C direction (not discussed in this article), Java code calls a native method, which is implemented as a C native library function; the C code uses JNI to interpret its Java input types and build its Java output types.

The JVM is packaged as a shared library ("jvm.dll" in the Sun SDK on Windows platforms and "libjvm.so" on Solaris) and exposes JNI Proper and the Invocation API as public exports. As a runtime entity, a JVM is really just the JVM library linked to an executable C program. Any Java developer can code such a program, e.g., "myCtoJProgram.exe", shown in Figure 2. Interestingly, the famed SDK C program known as the "launcher" (java.exe on Windows, Java on Solaris) is written in just the same way (for more, see sidebar JNI Case Study: Java Launcher). Completing the picture are native libraries, such as "myNative.dll", whose functions can be called from Java; these libraries are linked to the runtime process alongside the JVM.

A C-to-Java JNI Design: The Zip Example
The launcher is the best-known example of a C program that uses the JNI's C-to-Java interface; its purpose is to house a JVM and bootstrap a Java application on the JVM by calling the application's main method. Other, less obvious examples are C programs that require functionality whose best or only implementation is Java objects that must be called "in process." Examples include programs that:

  • Create, extract, or list the contents of zip files. The Java SDK java.util.zip package is the most suitable API available.
  • Transform XML to XML, HTML, or PDF. Though C XML APIs exist, Java's support for XML is vastly superior. Launching a JVM to do XML with Java is a plausible strategy for a C program.
  • Call an Enterprise JavaBean (EJB). The C program uses JNI to execute standard EJB client-side logic.
The example of listing the contents of a zip file highlights the coding challenge of C-to-Java JNI. (Source code for this article can be downloaded from www.sys-con.com/java/sourcec.cfm.) The Java code to perform this logic, shown in Listing 1, is trivial: line 4 instantiates the class ZipFile in java.util.zip, passing the zip file name to the constructor; lines 5-9 loop over a java.util.Enumeration of java.util.zip.ZipEntry objects getting, in line 8, the name of each entry in the zip file.

Developing the equivalent logic in C requires hundreds of lines of code. The main steps are summarized in Table 1. Listing 2 shows the code for steps 7 and 8; the JNI calls in lines 5, 10, 15, and 21 are followed by calls in lines 7, 11, 16, and 22 to the checkException() function (implementation not shown), which in turn calls the JNI exception handling functions ExceptionCheck(), ExceptionDescribe(), and ExceptionClear() to swallow and report Java exceptions triggered by the JNI calls.

The C code in the zip example can be made easier and less cumbersome by using two design patterns:

  1. Java Proxy: Put the hard code where it belongs, on the Java side. Develop a Java object, called a proxy, that performs complex logic on behalf of the C code. The C code need only call the proxy.
  2. C Façade: Hide JNI's peculiar programming model in a C façade library. Have the C program call the façade rather than JNI directly. In addition, build proxy support into the façade; expose façade functions to call the proxy.
The proxy and façade constitute an abstract framework for use in any program resembling the zip program. Java proxies implement the interface shown in Listing 3; the execute() method is defined generically as accepting an input, performing some action or set of actions, and returning an output. Listing 4 shows the proxy implementation for the zip example. The execute() method expects as input a string specifying the name of the zip file (line 26); it implements logic similar to that in Listing 1 to enumerate the entries in the zip file (lines 26-33) and returns a string containing the name of the entries in a pipe-delimited list (see lines 30-32 and 35). The method could also have returned an array or Java collection type, but the calling C program is likely happier parsing a string than contending with JNI array or collection class iteration logic.

If the C zip program were to call the proxy using JNI directly, its length would be shorter but the complexity of JNI would remain. Using the façade reduces the length even further and shields the code from JNI oddities. A design for the façade is depicted in Figure 3. The façade consists of a set of data types, modeled as C structures, and a set of functions. The data types represent entities such as JVM (cjJVM_t), class (cjClass_t), method (cjMethod_t), and object (cjObject_t). The functions are operations performed on the entities (e.g., cjJVMConnect() and cjJVMDisconnect() performed on the JVM), and a special set of proxy operations. Table 2 describes and lists the JNI usage of each of the C functions.

Listing 5 is the complete source code of the C zip program that uses the proxy and façade. (Listing 5 can be downloaded from www.sys-con.com/java/sourcec.cfm.) The program launches a JVM (line 26), gets a reference to the proxy class (line 29), instantiates it (line 33), and calls the execute() method (line 36). The remaining code (lines 40-50) cleans up the proxy object and class and destroys the JVM.

Conclusion
Hosting the hard logic in a Java proxy and wrapping JNI calls to the proxy in a C façade reduces the complexity of C-to-Java programming with JNI.

References

  • "Java Native Interface Specification," Sun Microsystems: java.sun.com/j2se/1.5.0/guide/jni/spec/jniTOC.html
  • "Tutorial on JNI," Sun Microsystems: java.sun.com/docs/books/tutorial/native1.1/concepts/index.html

    SIDEBAR

    JNI Case Study: Java Launcher
    Every Java developer who uses the Sun SDK is grateful for the C program known as the launcher (its executable name is "java"), which uses the JNI Invocation API to create a JVM, load a Java class into the JVM, and call its main() method, thereby launching a Java application on behalf of the caller. If the launcher did not exist, a good C developer with JNI knowledge could write an equivalent program in less than a week.

    The launcher's source code is available from Sun and is packaged with the SDK (version 1.4.2_04, which can be downloaded from java.sun.com/j2se/1.4.2/download.html). In the base directory of the installed SDK is a file called src.zip. If you extract that file, the exploded "launcher" directory contains the four source files that constitute the launcher program: java.h, java.c, java_md.h, and java_md.c. The launcher's source code is unmistakably C: murky, idiomatic, and circuitous. On the other hand, the end result is a functional program that has been run successfully innumerable times by innumerable users. Understanding how it works is a case study in the use of JNI.

    Suppose there is a class called Hi in the package com.mike that has a public main() method and thus can be started as a Java application through the launcher. The following is the source code:

    
    1	package com.mike;
    2	
    3	Public class Hi
    4	{
    5	   public static void main(String args[])
    6	   {
    7	      ...
    8	   }
    9	}
    

    The Hi application is started under SDK 1.4.2_04 on Windows 2000 with the following commands:

    1 set JAVA_HOME=c:\j2sdk1.4.2_04
    2 set _JAVA_LAUNCHER_DEBUG=true
    3 %JAVA_HOME%\bin\java -classpath src -Xms32m -Xmx64m -Dmy.property=1 com.mike.Hi arg1 arg2

    Line 3 calls the launcher executable Java in the bin directory of my SDK (which, as line 1 indicates, is c:\j2sdk1.4.2_04). The arguments passed to the launcher are:

    • -classpath src: Look for my "Hi" class in the directory src.
    • -Xms32m: Set its minimum heap size to 32MB.
    • -Xmx64m: Set the maximum heap size to 64MB.
    • -Dmy.property=1: Make a property available to the application with the key my.property and value 1.
    • com.mike.Hi: Run the application in this class.
    • arg1 arg2: Pass arguments "arg1" and "arg2" to the application's main method.
    Line 2 sets an environment variable called _JAVA_LAUNCHER_DEBUG, which causes the launcher to generate debugging output to the console at runtime:

    
    1	----_JAVA_LAUNCHER_DEBUG----
    2	JRE path is c:\j2sdk1.4.2_04\jre
    3	jvm.cfg[0] = ->-client<-
    4	jvm.cfg[1] = ->-server<-
    5	jvm.cfg[2] = ->-hotspot<-
    6	jvm.cfg[3] = ->-classic<-
    7	jvm.cfg[4] = ->-native<-
    8	jvm.cfg[5] = ->-green<-
    9	1306 micro seconds to parse jvm.cfg
    10	JVM path is c:\j2sdk1.4.2_04\jre\bin\client\jvm.dll
    11	5571 micro seconds to LoadJavaVM
    12	JavaVM args:
    13	    version 0x00010002, ignoreUnrecognized is JNI_FALSE, nOptions is 6
    14	    option[ 0] = '-Djava.class.path=.'
    15	    option[ 1] = '-Djava.class.path=src'
    16	    option[ 2] = '-Xms32m'
    17	    option[ 3] = '-Xmx64m'
    18	    option[ 4] = '-Dmy.property=1'
    19	    option[ 5] = '-Dsun.java.command=com.mike.Hi arg1 arg2'
    20	125113 micro seconds to InitializeJVM
    21	Main-Class is 'com.mike.Hi'
    22	Apps' argc is 2
    23	    argv[ 0] = 'arg1'
    24	    argv[ 1] = 'arg2'
    25	32937 micro seconds to load main class
    26	----_JAVA_LAUNCHER_DEBUG----
    

    The launcher begins by finding the JRE (line 2) and the right JVM (lines 3-10), and then loads the JVM dynamically (line 11); as we'll see, this logic is platform dependent. In this case, because we didn't name a specific JVM when calling the launcher, the launcher defaults to "client" (more on this below). Lines 12-19 show the arguments that the launcher will pass to the JVM; these correspond to the arguments passed to the launcher. In line 20, the launcher starts the JVM. The class whose main method that launcher will call is given on line 21; the arguments passed to it are shown in lines 22-24.

    The launcher's logical design (as of version 1.4.2_04) is depicted in Figure 4.

    The launcher consists of two modules: java.c, which contains the main function of the launcher program as well as platform-independent helper functions, and java_md.c, which houses platform-specific functions. The modules share a bidirectional dependency: func-tions in java.c call java_md.c and vice versa; for example, main() in java.c calls CreateExecutionEnvironment() in java_md.c, which in turn calls ReadKnownVMs() in java.c.

    The source code in java_md.c is different for each SDK platform release. For example, the Windows SDK has the Windows version of java_md.c but does not have the Solaris version. If you want to see both (as I did as I was writing this article), you must download both releases.

    The main steps in the launcher's processing are the following:

    1.  Create the execution environment: The CreateExecutionEnvironment() function, implemented in java_md.c, is a platform-specific search for the JRE path, JVM path, and JVM type for use by the launcher. The Windows version looks up the JRE path in the registry (on my machine, the registry key HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Edition\1.4\JavaHome is C:\Program Files\Java\j2re1.4.2_04), and then checks whether the JVM type selected by the caller (corresponding to "-client" or "-server" launcher command-line options) is one of the allowable types listed in the file JREPath\lib\Arch\jvm.cfg (on my machine "Arch" is "i386"). If the caller did not specify a JVM type, the launcher defaults to the first type listed in jvm.cfg (on my machine it's "client"). The JVM path on Windows is JREPath\bin\JVMType\jvm.dll (e.g., JREPath\bin\client\jvm.dll for the client JVM).

    2.  Load the JVM dynamically: Whereas most programs let the operating system implicitly link shared libraries to their processes, the launcher, which allows the user to specify at runtime which version of the JVM library to use (the "-client" or "-server" command-line arguments to the launcher), explicitly loads the JVM library using a platform-specific interface. The logic resides in java_md.c's LoadJavaVM() function. On Windows, this function calls the Win32 LoadLibrary() to load the JVM DLL and link it to the launcher process, and then calls the Win32 GetProcAddress() function to get a pointer to the invocation API function JNI_CreateJavaVM() used in step 4.

    3.  Prepare JVM runtime options based on command-line options passed to the launcher: Command-line options such as -D, -X, and -classpath are assembled into an array to be passed to the JVM. The launcher adds an additional property for use by the JVM of the form -Djava.sun.command=class arg1 arg2 ..., where class is the fully qualified name of the target class and "arg1 arg2 ..." is the list of command-line arguments to be passed to its main method.

    4.  Create the JVM: The launcher calls the JVM's JNI_Create-JavaVM() function, passing the options prepared above.

    5.  Load the target class into the JVM by calling the JNI FindClass() method: The launcher first replaces dots with slashes in the class name (e.g., it converts com.mike.Hi to com/mike/Hi) because the JVM expects slashes instead of dots.

    6.  Call the main method of the target class: First, the launcher gets a reference to the main() method by calling the JNI GetStaticMethodID() function, passing the class reference acquired in step 5, the method name ("main"), and the signature ("([Ljava/lang/String;)V", the JVM's peculiar representation of a void method that accepts an array of java.lang.String objects). Second, the launcher calls the method via CallStaticVoidMethod(). The launcher prepares the string array method input using some of the JNI's array functions (NewObjectArray(), SetObjectArrayElement()), and handles exceptions in the main method using JNI's ExceptionOccurred(), ExceptionDescribe(), and ExceptionClear().

    7.  Shutdown the JVM: This is done by calling the JNI DetachCurrent-Thread() and DestroyJavaVM() functions.

    A mystery to many Java developers, the launcher is nothing more than a little C program that uses the JNI to initiate a Java application.

  • More Stories By Michael Havey

    Michael Havey is a Chordiant consultant with 10 years of industry experience, mostly with application integration. Michael's book Essential Business Process Modeling was published by O'Reilly in August 2005.

    Comments (5) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    Most Recent Comments
    Bubb 07/26/07 08:47:36 PM EDT

    I got file not found when I accessed
    www.sys-con.com/java/sourcec.cfm

    DHAYANITHI R 03/23/05 03:01:47 AM EST

    Hi,
    Your matirial is good, But still now I can't understand how i call java function from C. Can u give some model source code, it is very usefull for me

    Michael 10/13/04 09:30:30 AM EDT

    It is really great article!!
    Is it possible to run on the Unix?

    Thanks,
    Michael

    pete 09/22/04 04:02:15 AM EDT

    it indeed is a very nice article.it helped me a lot for my assignment

    Jesse Barnum 08/21/04 10:45:44 PM EDT

    Thanks for this tutorial - it's exactly what I've been looking for. Everything that I see on JNI is for calling C from Java - this article is the other way around, which is just what I need to do.

    @ThingsExpo Stories
    SYS-CON Events announced today that Harbinger Systems will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Harbinger Systems is a global company providing software technology services. Since 1990, Harbinger has developed a strong customer base worldwide. Its customers include software product companies ranging from hi-tech start-ups in Silicon Valley to leading product companies in the US and large in-house IT organizations.
    SYS-CON Events announces a new pavilion on the Cloud Expo floor where WebRTC converges with the Internet of Things. Pavilion will showcase WebRTC and the Internet of Things. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devices--computers, smartphones, tablets, and sensors – connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades.
    The only place to be June 9-11 is Cloud Expo & @ThingsExpo 2015 East at the Javits Center in New York City. Join us there as delegates from all over the world come to listen to and engage with speakers & sponsors from the leading Cloud Computing, IoT & Big Data companies. Cloud Expo & @ThingsExpo are the leading events covering the booming market of Cloud Computing, IoT & Big Data for the enterprise. Speakers from all over the world will be hand-picked for their ability to explore the economic strategies that utility/cloud computing provides. Whether public, private, or in a hybrid form, clo...
    SYS-CON Events announced today that Gridstore™, the leader in software-defined storage (SDS) purpose-built for Windows Servers and Hyper-V, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Gridstore™ is the leader in software-defined storage purpose built for virtualization that is designed to accelerate applications in virtualized environments. Using its patented Server-Side Virtual Controller™ Technology (SVCT) to eliminate the I/O blender effect and accelerate applications Gridsto...
    SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
    As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at Internet of @ThingsExpo, Andrew Bolwell, Director of Innovation for HP’s Printing and Personal Systems Group, will discuss how key attributes of mobile technology – touch input, senso...
    The Internet of Things (IoT) is making everything it touches smarter – smart devices, smart cars and smart cities. And lucky us, we’re just beginning to reap the benefits as we work toward a networked society. However, this technology-driven innovation is impacting more than just individuals. The IoT has an environmental impact as well, which brings us to the theme of this month’s #IoTuesday Twitter chat. The ability to remove inefficiencies through connected objects is driving change throughout every sector, including waste management. BigBelly Solar, located just outside of Boston, is trans...
    Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, will examine three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective storage designed to handle the massive surge in back-end data in a world where timely analytics...
    Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
    The Internet of Things (IoT) promises to evolve the way the world does business; however, understanding how to apply it to your company can be a mystery. Most people struggle with understanding the potential business uses or tend to get caught up in the technology, resulting in solutions that fail to meet even minimum business goals. In his session at Internet of @ThingsExpo, Jesse Shiah, CEO / President / Co-Founder of AgilePoint Inc., will show what is needed to leverage the IoT to transform your business. He will discuss opportunities and challenges ahead for the IoT from a market and tec...
    SYS-CON Events announced today that TeleStax, the main sponsor of Mobicents, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. TeleStax provides Open Source Communications software and services that facilitate the shift from legacy SS7 based IN networks to IP based LTE and IMS networks hosted on private (on-premise), hybrid or public clouds. TeleStax products include Restcomm, JSLEE, SMSC Gateway, USSD Gateway, SS7 Resource Adaptors, SIP Servlets, Rich Multimedia Services, Presence Services/RCS, Diame...
    From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.
    SYS-CON Events announced today that O'Reilly Media has been named “Media Sponsor” of SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. O'Reilly Media spreads the knowledge of innovators through its books, online services, magazines, and conferences. Since 1978, O'Reilly Media has been a chronicler and catalyst of cutting-edge development, homing in on the technology trends that really matter and spurring their adoption by amplifying "faint signals" from the alpha geeks who are creating the future. An...
    The Transparent Cloud-computing Consortium (abbreviation: T-Cloud Consortium) will conduct research activities into changes in the computing model as a result of collaboration between "device" and "cloud" and the creation of new value and markets through organic data processing High speed and high quality networks, and dramatic improvements in computer processing capabilities, have greatly changed the nature of applications and made the storing and processing of data on the network commonplace.
    SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
    The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
    As a disruptive technology, Web Real-Time Communication (WebRTC), which is an emerging standard of web communications, is redefining how brands and consumers communicate in real time. The on-going narrative around WebRTC has largely been around incorporating video, audio and chat functions to apps. In his session at Internet of @ThingsExpo, Alex Gouaillard, Founder and CTO of Temasys Communications, will look at a fourth element – data channels – and talk about its potential to move WebRTC beyond browsers and into the Internet of Things.
    SYS-CON Events announced today that Gigaom Research has been named "Media Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Ashar Baig, Research Director, Cloud, at Gigaom Research, will also lead a Power Panel on the topic "Choosing the Right Cloud Option." Gigaom Research provides timely, in-depth analysis of emerging technologies for individual and corporate subscribers. Gigaom Research's network of 200+ independent analysts provides new content daily that bridges the gap between break...
    We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
    The Industrial Internet revolution is now underway, enabled by connected machines and billions of devices that communicate and collaborate. The massive amounts of Big Data requiring real-time analysis is flooding legacy IT systems and giving way to cloud environments that can handle the unpredictable workloads. Yet many barriers remain until we can fully realize the opportunities and benefits from the convergence of machines and devices with Big Data and the cloud, including interoperability, data security and privacy.