Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ghostravenstorm · Oct 07, 2021 at 06:21 PM · windowsjavajni

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");
    Debug.Log(strLen);
    /// Outputs 2 on Android.
    /// Outputs 0 on Windows.
 }
 catch (Exception e) {
    /// No exception is caught.
    Debug.LogError(e);
 }

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.


UPDATE:
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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nk111111 · Oct 24, 2021 at 03:48 PM 0
Share

I have the same problem. Did you find an answer @ghostravenstorm ? I don't understand why I just get a 0 from the Java call, and no error is visible anywhere... I can even remove the jar from the Plugins/Android folder and it still behaves the same way...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 22, 2021 at 10:36 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass a JNI C# class into Java? 0 Answers

Call non-static method (from subclass) in Java from Unity 1 Answer

Calling a java method from a second thread is not working 1 Answer

JNI error when calling Java plugin from System.AppDomain.CurrentDomain.UnhandledException callback handler 0 Answers

Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges