How use the Java Native Interface in Unity outside of Android?

I have a third party library I want to implement into my Unity projects. It’s buitl in Java so I have a JAR file I can throw into my project as a plugin. But I’m not developing for Android. This is purely a Windows project.

To test the waters, I’m trying to conduct the following:
I’m trying to 100% confirm that using the Java Native Interface in Unity doesn’t work on any platform other than Android.
Unity’s API is labeled as being Android specific, but I’m trying to tool around with the API to see if I can have Unity execute non-Android Java code on Windows.

Here’s an example:

try {
   /// In Java Code: String str = new String("HI");
   var str = new AndroidJavaObject("java.lang.String", new object[]{"HI"});
   /// In Java Code: int strLen = str.length();
   var strLen = str.Call("length");
   /// Outputs 2 on Android.
   /// Outputs 0 on Windows.
catch (Exception e) {
   /// No exception is caught.

If this absolutely doesn’t work, I’d expect Unity to throw a compile error, editor error, runtime error, or runtime exception. Instead nothing happens.
Unity seems to create my Java String object without problems, but when I try to access it’s properties on Windows, they’re zeroed out.
That means AndroidJavaObject isn’t invoking the String constructor as expected or it’s silently failing due to some unknown reason. This unknown reasons is what I want to know, or why the object contructor isn’t being invoked.


I tried writing some custom Java code to see if just calling a method even returns a value instead of trying to create Java primitives on the Unity side.

I made a Java class and exported it to a jar file and imported that into Unity and ran the following code:

/// JAVA:
package com.vcom3d;

public class DoTheThing {
   public int GetTheThing () {
      return 5;

/// C#:
var doTheThing = new AndroidJavaObject("com.vcom3d.DoTheThing");
var num = doTheThing.Call<int>("GetTheThing");
Debug.Log(num); /// Outputs 0

This behavior of any returned data being zeroed or null is very baffling. I can’t tell if this is suppose to work or not because it’s not throwing any error telling me otherwise.

No, that’s not possible. JNI is just a native interface into the java runtime in which Unity actually runs when build for android. As you may know Android uses the dalvik java VM to run apps. Unity does not ship a java runtime and a windows build would not have any reason to have any kind of java runtime running in the back that you can interface with. A lot prople nowadays don’t even have any java installed on their machines.

Note that JNI in general is not really an interprocess interface but rather an interface to run native code from inside a java application and also allow those native calls to interface with the underlying JVM. So it’s mainly meant for applications that are fundamentally a java application and that will run native code. This is what Unity does on Android. An app is a java application and it will interface with Unity and Unity can call back to java.

Note that it’s generally possible to run your own java VM inside a native application. However for this you have to dig deep into java and the java VM and manually create a VM. Unity’s AndroidJNI interface won’t really help here.