Click here to close now.


Java IoT Authors: Jnan Dash, Scott Allen, Bob Gourley, Dana Gardner, Pat Romanski

Related Topics: Java IoT

Java IoT: Article

Embedding the Java Virtual Machine, Once And For All

A solution with complete support

JTL is a portable C++ JNI template library that allows you to embed a JVM within another program. An example of this might be found inside a browser that needs to support Java plug-ins. First I'll discuss the traditional way to embed a JVM within a native program, and then talk about the generic solution offered by JTL.

To apply the solution provided by JTL, you need to know the basics of Java, C++, and JNI. However, to understand JTL's design you should be familiar with modern C++ techniques, such as template programming and the Boost library (

All the sample code in this article is written for Microsoft Windows, but it can easily be ported to other platforms.

The Traditional Way to Embed the JVM

Although there are lots of resources on how to embed a JVM, most of them are based on Sheng Liang's classic book, Java Native Interface: Programmer's Guide and Specification. Figure 1 shows the basic procedure.

If the application has other threads that are using the JVM as well, the communications among these threads will look like Figure 2.

Listing 1 shows the typical code (all the error checking and exception handling code in this article has been omitted for clarity). This code works fine as long as your program is simple enough; however, there are three major drawbacks.

Issue 1: The JVM is a global variable, which is not acceptable in some cases.

Issue 2: The threads in which jvm and env are used and the thread that launches jvm couple tightly. For example, there will be a potential timing issue that's clearly shown in Figure 2. The AttachCurrentThread(...) must be called after the launcher thread initialized jvm, and DetachCurrentThread() must be called before jvm has been destroyed; otherwise this creates unnecessary communications overhead between threads. It gets worse when you don't know which thread is the launcher thread.

Of course the VM can always be created in one thread when the native application starts. However, this is a pure waste if there is no Java client. The JVM should not be created if there are no requests.

Issue 3. The function calls to Attach-CurrentThread(...) and Detach-Current Thread() should be paired so the JVM can get a chance to free the local references. However, it's too easy to not have the DetachCurrentThread() called by the programmer (because of multiple flow paths) or by exceptions. A better approach would be to encapsulate the JVM into a class or classes.

Thin JVM Wrapper

It's natural to wrap the JavaVM* pointer into a member variable, say jvm. Because only one JVM in each process can be created (until JDK 1.4.2), the obvious class design is to make jvm static. Listing 2 shows the first attempt.

Here the method's signatures are not important so let's focus on the class design. The thin wrapper approach is simple and straightforward; however, it's not much better than the C-like code in Listing 1. It only gets rid of the explicit global variable jvm (the static member variable is still kind of global though). Issues 2 and 3, mentioned in the previous section, still need to be addressed.

Singleton JVM

The Singleton design pattern is described in Design Patterns by Erich Gamma, et al, as "Ensure a class only has one instance, and provide a global point of access of it." In other words, a Singleton class is a class for which no more than one instance can exist at runtime. This is what the JVM behaves like. We can apply the Singleton pattern to the JVM class design.

Andrei Alexandrescu has provided all kinds of singleton implementations in his book Modern C++ Design and the Loki library. Loki has a singleton manager class template, SingletonHolder, that looks like Listing 3.

The template parameter T in Listing 3 is the target class, in our case CJavaVM. Other template parameters specify the policies that manage the singleton. As explained in Alexandrescu's book, "A policy defines a class interface or a class template interface. The interface consists of one or all of the following: inner type definitions, member functions, and member variables." Policy classes are not intended for stand-alone use and their member functions are often static. In Loki::-Single-tonHolder, the CreationPolicy determines how the singleton instance is created and destroyed, and the Threading-Model policy determines whether the singleton is living in a multiple threaded world.

Listing 4 provides the redesigned CJavaVM class and its usage with Loki::-SingletonHolder. A difference between CJavaVM and CJavaVM2 is that all members in CJavaVM2 are not static. Listing 4 looks fancier than Listing 1. However, it does not solve the real problems. Issues 2 and 3 are still unresolved. For example, if there are any other threads trying to make a JNI call, it's still a requirement that the JVM launcher thread has already called jvm.Start-JavaVM(). This is because CJavaVM2's constructor does nothing. If StartJava-VM() can be moved into CJavaVM2's constructor and Destroy-JavaVM() can be moved into CJavaVM2's destructor, then the timing issue will be solved, which is not easy to do.

The major difficulty is that Loki::-SingletonHolder's template parameter CreationPolicy only calls the target class T's default constructor. In other words, CJavaVM2's constructor cannot look something like this: CJavaVM2(std::-string- jvmpath, JavaVMInitArgs& args). It must be CJavaVM2(). Period.

We are not out of bullets though. The "Every problem can be solved by adding another layer of indirection" idiom applies here.

Policy-Based JVM Class

The solution is to make CJavaVM2 policy-based too. Listing 5 shows the new version.

In CJavaVM3, a new policy class JavaVMLauncher is introduced and declared as follows:

class JavaVMLauncher {
    static void LaunchJavaVM(void** ppLib, JavaVM** ppJavaVM);
    static void DestroyJavaVM(void* pLib, JavaVM* pJavaVM);

ppLib is a pointer to a handle returned by LoadLibrary on Windows or dlopen on Solaris. To make JavaVMLauncher more generic, the void pointer is used here. The pointer to pointer method is used for ppLib and ppJavaVM in LaunchJavaVM() because JavaVMLauncher does not contain any member variables. It's the caller to provide the placeholders for the objects pointed to by ppLib and ppJavaVM. DestroyJavaVM() simply accepts the cached JavaVM* and lib handle to destroy the JVM and free the library.

Now we can invoke the JVM as in Listing 6, which is pretty much what JTL does. In JTL, there is another class template called JavaVMMgr that is the placeholder for the JavaVM* and pLib. All the JVM calls will be delegated to the JavaVMMgr.

Exception Handling

The LaunchJavaVM() method in JavaVMLauncher can fail; for example, if the user does not have a JRE installed on his machine. When this happens, LaunchJavaVM() can return a flag or error code. In JTL, it will simply throw a jtl::load_javavm_error exception.

Multithreading, Extending Loki, and boost::thread

Remember in Listing 3, Loki::Single-tonHolder has a template parameter ThreadingModel that's used to synchronize the calls to MakeInstance(). However, under different platforms the thread implementation and API are different. To make the JVM class design platform portable, it needs to use a portable threading framework. JTL uses boost::thread because it's simple and easy to use, and it will likely be part of the next version of the standard C++ library.

Though Alexandrescu provides us with the excellent SingletonHolder class, JTL does not use it directly for two main reasons: not all compilers support Loki well and Loki::ThreadingModel is only implemented to work with Windows.jtl::SingletonHolder does some extension to Loki::SingletonHolder to achieve the platform-portable goal. However, jtl::SingletonHolder uses the same template parameters (and in the same order) as Loki::SingletonHolder, therefore the user can easily switch to Loki's version if someday Loki provides us with a platform-portable ThreadingModel.

Another thing needs to be mentioned for ThreadingModel - it should be able to work with both the single-thread model and multiple-thread model. If there's only one thread, there's no sense in doing the synchronization. To achieve this generic solution, JTL provides two class templates, Multiple-ThreadModel and SingleThread-Model, as shown in Listing 7.

These two classes simply provide type definitions. In Mul-tipleThread-Model, JTL uses the boost::mutex and boost::mutex::scoped_lock as the synchronization primi-tives, while in Single-ThreadModel it uses boostex::-faked_-mutex and boostex::faked_mutex::-scoped_-lock as the synchronization primitives. The faked mutex and scoped_lock are simple extensions to boost mutex and scoped_lock. They're empty classes and provide only the required inter-face methods, which are implemented as noop. This is a common trick in generic programming. (ATL programmers will recall the CComFake-CriticalSection used in CCom-Single-Thread-Model and CComMultiThread-Model that does the same trick.)

JNIEnv Smart Pointers

In Listing 6, we solved issues 1 and 2; we no longer have a global variable, and we don't have a timing issue when calling AttachCurrentThread() and Detach-CurrentThread(). However, issue 3 - how to ensure that DetachCurrent-Thread() is called - is still unresolved.

When doing JNI programming, most of the time we're dealing with the JNIEnv* pointer rather than the JavaVM* pointer. The most important characteristic of the JNIEnv pointer is that it has thread affinity (it's only valid in its associated thread). It would be nice if we could get the JNIEnv pointer in an arbitrary context. To achieve this and solve issue 3, JTL provides three smart pointers: simple_env_ ptr, auto_env_ptr, and thread_env_ptr. They're all class templates.

Before talking about these smart pointers, let's review the scenarios in which the JNIEnv pointer is used:

  1. In the launcher thread: In this case the ThreadingModel template parameter should be SingleThread-Model to avoid unnecessary thread synchronization, even though there may be multiple threads.
  2. In multiple threads: AttachCurrent-Thread() and Detach-CurrentThread() will be called only once for each thread.
  3. In multiple threads: AttachCurrent-Thread() and Detach-CurrentThread() can be called multiple times for each thread (see below).
simple_env_ptr, auto_env_ptr and thread_env_ptr are corresponding solutions for scenarios 1, 2, and 3. All these smart pointers are derived from env_ ptr_base, which is a placeholder for JNIEnv* pointer and has other helper functions, such as operator->(), operator!(). By the way, all three JNIEnv smart pointers are not full-blown. For more details about smart pointers, please refer to Alexandrescu's book.

The simple_env_ptr is defined as the following:

template < ... >
class simple_env_ptr : public env_ptr_base {
    //... typedefs for SingletonJVM;
    simple_env_ptr() {
        SingletonJVM::Instance().GetEnv(&env_, jvm_version);
    ~simple_env_ptr() { env_ = 0;}

In simple_env_ptr's constructor, the JNIEnv pointer has been initialized by calling GetEnv(). This works because the smart pointer is in the launcher thread. Similarly, auto_env_ptr is defined as the following:

template < ... >
class auto_env_ptr : public env_ptr_base {
    //... typedefs for SingletonJVM;
    auto_env_ptr() {
        SingletonJVM::Instance().AttachCurrentThread(&env_, 0);
    ~auto_env_ptr() {
        if(env_) {
            env_ = 0;
}; // auto_env_ptr

As you have seen, auto_env_ptr's constructor and destructor do the job of attaching and detaching the current thread. Thus we have solved issue 3 and we don't need to worry about the missing detaching call. However, auto_env_ ptr may not work if there is more than one auto_env_ptr instance in the same thread. This is clear in the following snippet:

// auto_env_ptr ptr1, ptr2
// ...other stuff
// ... from here all calls on ptr1 may be invalid

In this snippet, when ptr2 is out of scope, its destructor will be called, causing the current thread to detach from the JVM. This may cause problems because ptr1 is still active; however, all its local references may have been freed by the JVM because of the ptr2's detaching call.

To solve this problem, JTL provides thread_env_ptr, which is defined in Listing 8. thread_env_ptr has a counter that's a thread local storage variable. Whenever there is a new thread_env _ptr instance, the counter will be bumped by one. Whenever there is a thread_env_ptr out of scope, the counter will be decreased by one. When the counter is decreased to zero, the current thread will be detached from the JVM.

Putting It All Together

Listing 9 illustrates how to use the JTL JVM invocation. You may provide your own JVMLauncher if the default one does not meet your requirement.


JTL provides complete support for the JVM invocation. It also provides JNIEnv smart pointers to get the JNIEnv* pointer in arbitrary context. Most of the classes in JTL are designed as class templates and they can easily be extended. JTL provides many template parameter implementations as well, which can be used as the default template parameter value under most circumstances.

All comments about JTL and this article are highly appreciated.

More Stories By Stanley Wang

Stanley Wang is a lead software developer at Vcom3D, Inc., and the author of JTL. He received his MS in computer science from the University of Florida. He is interested in system programming, generic programming, and computer graphics.

Comments (0)

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.

@ThingsExpo Stories
Most people haven’t heard the word, “gamification,” even though they probably, and perhaps unwittingly, participate in it every day. Gamification is “the process of adding games or game-like elements to something (as a task) so as to encourage participation.” Further, gamification is about bringing game mechanics – rules, constructs, processes, and methods – into the real world in an effort to engage people. In his session at @ThingsExpo, Robert Endo, owner and engagement manager of Intrepid D...
With an estimated 50 billion devices connected to the Internet by 2020, several industries will begin to expand their capabilities for retaining end point data at the edge to better utilize the range of data types and sheer volume of M2M data generated by the Internet of Things. In his session at @ThingsExpo, Don DeLoach, CEO and President of Infobright, will discuss the infrastructures businesses will need to implement to handle this explosion of data by providing specific use cases for filte...
SYS-CON Events announced today that Fusion, a leading provider of cloud services, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. Fusion, a leading provider of integrated cloud solutions to small, medium and large businesses, is the industry's single source for the cloud. Fusion's advanced, proprietary cloud service platform enables the integration of leading edge solutions in the cloud, including clou...
SYS-CON Events announced today that Commvault, a global leader in enterprise data protection and information management, has been named “Bronze Sponsor” of SYS-CON's 18th International Cloud Expo, which will take place on June 7–9, 2016, at the Javits Center in New York City, NY, and the 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Commvault is a leading provider of data protection and information management...
SYS-CON Events announced today that Alert Logic, Inc., the leading provider of Security-as-a-Service solutions for the cloud, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. Alert Logic, Inc., provides Security-as-a-Service for on-premises, cloud, and hybrid infrastructures, delivering deep security insight and continuous protection for customers at a lower cost than traditional security solutions. Ful...
SYS-CON Events announced today that VAI, a leading ERP software provider, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. VAI (Vormittag Associates, Inc.) is a leading independent mid-market ERP software developer renowned for its flexible solutions and ability to automate critical business functions for the distribution, manufacturing, specialty retail and service sectors. An IBM Premier Business Part...
The cloud promises new levels of agility and cost-savings for Big Data, data warehousing and analytics. But it’s challenging to understand all the options – from IaaS and PaaS to newer services like HaaS (Hadoop as a Service) and BDaaS (Big Data as a Service). In her session at @BigDataExpo at @ThingsExpo, Hannah Smalltree, a director at Cazena, will provide an educational overview of emerging “as-a-service” options for Big Data in the cloud. This is critical background for IT and data profes...
Fortunately, meaningful and tangible business cases for IoT are plentiful in a broad array of industries and vertical markets. These range from simple warranty cost reduction for capital intensive assets, to minimizing downtime for vital business tools, to creating feedback loops improving product design, to improving and enhancing enterprise customer experiences. All of these business cases, which will be briefly explored in this session, hinge on cost effectively extracting relevant data from ...
As enterprises work to take advantage of Big Data technologies, they frequently become distracted by product-level decisions. In most new Big Data builds this approach is completely counter-productive: it presupposes tools that may not be a fit for development teams, forces IT to take on the burden of evaluating and maintaining unfamiliar technology, and represents a major up-front expense. In his session at @BigDataExpo at @ThingsExpo, Andrew Warfield, CTO and Co-Founder of Coho Data, will dis...
With the Apple Watch making its way onto wrists all over the world, it’s only a matter of time before it becomes a staple in the workplace. In fact, Forrester reported that 68 percent of technology and business decision-makers characterize wearables as a top priority for 2015. Recognizing their business value early on, was the first to bring ERP to wearables, helping streamline communication across front and back office functions. In his session at @ThingsExpo, Kevin Roberts...
SYS-CON Events announced today that Interoute, owner-operator of one of Europe's largest networks and a global cloud services platform, has been named “Bronze Sponsor” of SYS-CON's 18th Cloud Expo, which will take place on June 7-9, 2015 at the Javits Center in New York, New York. Interoute is the owner-operator of one of Europe's largest networks and a global cloud services platform which encompasses 12 data centers, 14 virtual data centers and 31 colocation centers, with connections to 195 ad...
Eighty percent of a data scientist’s time is spent gathering and cleaning up data, and 80% of all data is unstructured and almost never analyzed. Cognitive computing, in combination with Big Data, is changing the equation by creating data reservoirs and using natural language processing to enable analysis of unstructured data sources. This is impacting every aspect of the analytics profession from how data is mined (and by whom) to how it is delivered. This is not some futuristic vision: it's ha...
WebRTC has had a real tough three or four years, and so have those working with it. Only a few short years ago, the development world were excited about WebRTC and proclaiming how awesome it was. You might have played with the technology a couple of years ago, only to find the extra infrastructure requirements were painful to implement and poorly documented. This probably left a bitter taste in your mouth, especially when things went wrong.
Learn how IoT, cloud, social networks and last but not least, humans, can be integrated into a seamless integration of cooperative organisms both cybernetic and biological. This has been enabled by recent advances in IoT device capabilities, messaging frameworks, presence and collaboration services, where devices can share information and make independent and human assisted decisions based upon social status from other entities. In his session at @ThingsExpo, Michael Heydt, founder of Seamless...
The IoT's basic concept of collecting data from as many sources possible to drive better decision making, create process innovation and realize additional revenue has been in use at large enterprises with deep pockets for decades. So what has changed? In his session at @ThingsExpo, Prasanna Sivaramakrishnan, Solutions Architect at Red Hat, discussed the impact commodity hardware, ubiquitous connectivity, and innovations in open source software are having on the connected universe of people, thi...
WebRTC: together these advances have created a perfect storm of technologies that are disrupting and transforming classic communications models and ecosystems. In his session at WebRTC Summit, Cary Bran, VP of Innovation and New Ventures at Plantronics and PLT Labs, provided an overview of this technological shift, including associated business and consumer communications impacts, and opportunities it may enable, complement or entirely transform.
There are so many tools and techniques for data analytics that even for a data scientist the choices, possible systems, and even the types of data can be daunting. In his session at @ThingsExpo, Chris Harrold, Global CTO for Big Data Solutions for EMC Corporation, showed how to perform a simple, but meaningful analysis of social sentiment data using freely available tools that take only minutes to download and install. Participants received the download information, scripts, and complete end-t...
For manufacturers, the Internet of Things (IoT) represents a jumping-off point for innovation, jobs, and revenue creation. But to adequately seize the opportunity, manufacturers must design devices that are interconnected, can continually sense their environment and process huge amounts of data. As a first step, manufacturers must embrace a new product development ecosystem in order to support these products.
Manufacturing connected IoT versions of traditional products requires more than multiple deep technology skills. It also requires a shift in mindset, to realize that connected, sensor-enabled “things” act more like services than what we usually think of as products. In his session at @ThingsExpo, David Friedman, CEO and co-founder of Ayla Networks, discussed how when sensors start generating detailed real-world data about products and how they’re being used, smart manufacturers can use the dat...
When it comes to IoT in the enterprise, namely the commercial building and hospitality markets, a benefit not getting the attention it deserves is energy efficiency, and IoT’s direct impact on a cleaner, greener environment when installed in smart buildings. Until now clean technology was offered piecemeal and led with point solutions that require significant systems integration to orchestrate and deploy. There didn't exist a 'top down' approach that can manage and monitor the way a Smart Buildi...