To embed Java in a native application, use the Java Native Interface (JNI): a native host can create a Java Virtual Machine (JVM) and call Java code where the runtime supports it, while Java can call native functions exposed by a library. The right setup depends on the platform. Thierry Manfé’s 2001 InfoWorld tutorial demonstrates a C application on Solaris embedding Swing; Android apps use JNI too, but follow Android’s runtime and NDK guidance rather than those historical desktop steps.
What JNI lets a native app do
JNI is the interface between Java and native code such as C or C++. There are two directions across that boundary:
- Native to Java: Native code can create or use a JVM through the Invocation API, locate Java classes and methods, and invoke them. This is useful when a native host process needs Java behavior or a Java-based interface, provided its runtime supports JVM embedding.
- Java to native: A Java class declares a method with the
nativekeyword, loads a native library, and calls the implementation supplied by that library.
These are related capabilities, not identical application architectures. Oracle’s Java Native Interface Specification for Java SE 25 describes JNI types, references, method calls, native-method registration, exceptions, and the Invocation API.
How the 2001 Unix example embeds Swing
Thierry Manfé’s InfoWorld tutorial, “Embed Java code into your native apps,” published May 11, 2001, shows a C application starting a JVM and connecting a Java Swing menu to an existing native program. It is a historical Solaris desktop example, not an Android how-to.
#1 Best Overall
- 【Strong Adsorption】The inspiration of the silicone phone suction case comes from the adhesive force of the octopus. Each suction cup phone mount is 3.15 inches long and 2.17 inches wide, with 24 independent suction cups providing a stronger and more stable suction force, so you don't have to worry about your phone falling during use.
- 【Back of Phone Suction Grip】Remove the adhesive film on the phone suction cup and stick it on the phone case. You can then fix the phone on any smooth surface, which is very convenient. (The phone suction cup cannot be removed and reused after being attached to the phone case. It is recommended to attach it to a regular phone case, not a valuable one.)
- 【Widely Used】Our non-slip silicone phone sticky grip mount attaches to almost any flat phone case and make it compatible with common mobile phones such as iPhone and Android.You can shoot, watch videos or video calls in the kitchen, gym, dance studio, bathroom and other places.
- 【Capture the Wonderful Picture】Whether you are a TikTok creator or just like to share videos and photos, this phone suction cup can help you hands-free capture wonderful videos and photos for sharing with friends.
- 【Note】You can fix the phone suction cup on a smooth surface such as a mirror or glass. If necessary, wipe the suction cup with a damp cloth to obtain stronger suction. Before releasing your hand, make sure the phone is firmly fixed. (Not applicable to rough walls, wooden surfaces, and other uneven surfaces)
Starting the JVM and creating the Java object
The C program starts the JVM from its main function, supplies class and library paths, finds the SwingMenu class, obtains its constructor identifier, and creates an instance. This is the native-to-Java direction: the host application controls JVM startup and calls into Java.
Calling legacy code from a Swing action
The Swing action handler calls a Java method declared native, named changeColour in the example. Its C implementation then calls a function in the legacy application. That callback path lets a Java control trigger behavior already owned by the native program.
Rank #2
- SUPERIOR COMFORT — Unlike traditional circular ear buds, the design of EarPods is defined by the geometry of the ear. Which makes them more comfortable for more people than any other ear bud–style headphones.
- HIGH-QUALITY AUDIO — The speakers inside EarPods have been engineered to maximize sound output and minimize sound loss, which means you get high-quality audio.
- BUILT-IN REMOTE — EarPods with USB-C plug also include a built-in remote that lets you adjust the volume, control the playback of music and video, and answer or end calls with a pinch of the cord.
- COMPATIBILITY — Works with all devices that have a USB-C port.
- INTEGRATED MICROPHONE — A built-in microphone precisely captures your voice while you’re on the phone, taking a FaceTime call, or summoning Siri — so you’re always heard loud and clear.
Joining the GUI and graphics event loops
The example also integrates the Java window with Motif, X11, and AWT Native Interface, and coordinates Java-originated OpenGL operations with the legacy application. For graphics work, the author routes requests through a datagram socket so they can be handled by the legacy application’s main event loop. The useful architectural lesson is specific to this example: when the existing application owns a graphics context or event loop, direct cross-thread graphics calls can be risky, so route work to the thread that owns that state.
The tutorial identifies Solaris 7 or higher, JDK 1.2.2 or higher, and OpenGL 1.2.1 or higher as its example’s minimum environment. Those are the article’s 2001 requirements, not current compatibility recommendations.
Recommended Free Tools
Rank #3
- Secure Hold: Our PopSockets adhesive phone grip gives your cell phone a secure, comfortable hold in hand to help prevent drops while texting, taking photos, or scrolling on the go. Designed to stick firmly to most phone cases and devices.
- Hands-Free Made Easy: Easily turn your PopSocket into a phone stand to prop up your phone anywhere, perfect for watching videos, video calls, or following recipes. A must-have phone holder that keeps your device secure and ready for anything.
- Compatibility: Works with all phones, tablets, and Kindles. Sticks best to smooth, hard plastic cases and may not adhere to silicone or textured cases. Easily swap your PopTop to change up your style.
- Black PopSockets: Simple, refined, and endlessly versatile. A timeless essential for any phone.
- Travel Must-Have for People On the Go: A must-have travel accessory for flights, flying, airports, air travel, airplanes, planes, international trips, cruises, and long travel days. Key gadget for your airport haul, travel accessories and must-haves.
How Android JNI differs
JNI is also used by Android apps, but Android is not a native desktop host embedding Swing or starting a JVM with the Invocation API. In a typical Android app, Java or Kotlin code works with native C/C++ code through the app’s runtime and native libraries. Use the Android Developers NDK JNI Tips for Android-specific implementation details; do not carry over the Solaris, Motif, X11, or AWT Native Interface setup from the InfoWorld example.
For either context, first decide which side owns the operation. Keep boundary calls focused rather than bouncing frequently between Java and native code. Android Developers advises: “Keep your interface code in a low number of easily identified C++ and Java source locations to facilitate future refactors.”
Rank #4
- [360 ° Flexible Rotation Design] Comes with a rotatable lanyard ring that supports 360 ° free rotation, effectively solving the problem of twisted and tangled lanyards
- [Wide compatibility] The ultra-thin 0.02-inch design does not block the charging port at all, and both wired and wireless charging can be used directly without removing the pad. Compatible with most smartphones such as iPhone, compatible with various wristbands, lanyards, crossbody straps, and keychains
- [Durable and Portable Material] Premium rust-resistant stainless steel material with good flexibility, which not only avoids scratching the phone case, but also has excellent anti rust and anti fading performance
- [Multi scenario Practical] Paired with a lanyard or wristband, hands-free use can be achieved. The phone is within reach and not easily dropped, ideal for daily commuting and outdoor activities. Suitable for full coverage phone cases, does not support half coverage phone cases
- [Quality Service] If you find any damage or other issues with the product upon receipt, please contact us immediately. We will handle it quickly
JNI practices that prevent common Android bugs
Use the right JNI environment for each thread
JavaVM represents the process-level VM on Android, but JNIEnv belongs to the current thread. Never pass a JNIEnv pointer from one thread to another. A native-created thread must attach to the VM before making JNI calls and detach before it exits. When practical, use Java-created threads for work that needs to call Java.
Respect reference lifetimes
Local JNI references are valid only on the current thread during the native call in which they were created. If native code needs to retain an object after that call returns, create a global reference and delete it when it is no longer needed. The same care applies when retaining a class reference while caching method or field identifiers.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Best Value
- 【PKYAA Double Sided Silicone Suction Phone Case Mount】PKYAA With Double Sided 40 Strong and Reliable individual suction cups, PKYAA provides a thicken and upgraded universal silicon suction mount for your phone.
- 【Friendly to Content Creators】If you are a content creator or an online influencer, you can create videos anywhere with this suction mount completely hands free with this silicone cell phone mount for cases.
- 【HANDS-FREE & Adhere to Mirrors】This Double Sided silicone suction phone case mount allows you to stick your phone to the mirror easily. No longer holding your phone in one hand to watch video tutorials while making up.
- 【Strong Grip on the Smooth Surface】You can easily hang your phone anywhere with a smooth surface. All you do is you clean off your phone and smooth surface. It is STURDY and it not only sticks to mirrors, it also sticks to windows, it sticks to refrigerators, tiles and other clean, flat surfaces.
- 【Press Down Firmly Every 30 Minutes】Use your palm or fingers to press the phone down firmly and check it's secure before letting go. Apply even pressure for a few seconds to allow the suction cup to adhere properly. To maintain the grip and prevent accidental falls, it's a good practice to periodically reapply pressure to the suction cup.
Handle exceptions and release acquired data
Check for Java exceptions after calls into managed code and handle pending exceptions before making further JNI calls that require a clear exception state. Release string and array data acquired through JNI when finished. Failing to manage these resources or references can lead to leaks, invalid access, or hard-to-diagnose runtime failures.
Use CheckJNI during development
Android’s CheckJNI diagnostics can catch common mistakes, including using a JNIEnv from the wrong thread, using invalid references, or making calls while an exception is pending. Treat these checks as development safeguards, alongside deliberate review of reference and resource lifetimes.
Quick Recap
Choose the integration model that fits
| Question | Native host embedding a JVM | Android app using JNI |
|---|---|---|
| Which side starts or owns the runtime? | The native host starts and uses a JVM through the Invocation API where supported. | The Android app runs in Android’s runtime; Java or Kotlin code interoperates with native libraries. |
| Typical purpose | Add Java behavior or a Java UI to an existing native process. | Use or expose native C/C++ functionality within an Android app. |
| Platform integration to consider | Runtime availability, callback and thread design, and the host window system. Manfé’s example uses Solaris, Motif/X11, Swing, and AWT Native Interface. | Thread attachment, reference and resource lifetimes, exceptions, and Android NDK guidance. |
| Relevant guidance | Oracle Java SE 25 JNI Specification for the interface and Invocation API. | Android Developers NDK JNI Tips for Android implementation details. |
A practical design checklist
- Confirm which environment you are targeting: a native process embedding a JVM or an Android app calling native libraries.
- Keep Java/native crossings limited and make the boundary code easy to locate.
- For Android native threads, attach before JNI use and detach at thread exit; never share
JNIEnv. - Track local and global references, and delete retained global references when done.
- Check exceptions and release acquired strings and arrays.
- Use platform-appropriate diagnostics, including CheckJNI for Android development.

