Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:19:48+00:00 2026-06-02T00:19:48+00:00

I’m declaring an inline native JNI function, getId. I’m unsure of how to tell

  • 0

I’m declaring an inline native JNI function, getId. I’m unsure of how to tell SWIG, in the %native snippet below, how to just return a java.lang.Object. I have TBD there for now. I believe a typemap is the proper approach, but I’m unsure of how to declare it for a native JNI method using the %native function. Any ideas?

%module Sample
%{
JNIEXPORT jobject JNICALL Java_com_test_SampleJNI_getID(JNIEnv *env, jclass cls) 
{
    jmethodID cnstrctr;
    jvalue args[2];
    jobject obj;
    jbyteArray bArray;

    bArray = (*env)->NewByteArray(env, 32);

   (*env)->SetByteArrayRegion(env, bArray, 0, 32, (jbyte *)foo);

   cls = (*env)->FindClass(env, "com/test/jni/DeviceId");
   cnstrctr = (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String;[B)V");

   args[0].i = (*env)->NewStringUTF(env, "id");
   args[1].i = bArray;

   obj = (*env)->NewObjectA(env, cls, cnstrctr, args);

   return obj;
}
%}
%native(getID) TBD getID();

DeviceId.java:

package com.test.jni;

public class DeviceId {
    private String id;
    private byte[] cache;

    public DeviceId(String id, byte[] cache){
        this.id=id;
        this.cache=cache;
    }

    public byte[] getCache() {
        return cache;
    }

    public void setCache(byte[] cache) {
        this.cache = cache;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:19:50+00:00Added an answer on June 2, 2026 at 12:19 am

    You can use typemaps (e.g. jtype and jstype most likely) with %native, but you can also just say simply jobject if you have an Object being returned and have it handled for you, e.g.:

    %module Sample
    %{
    JNIEXPORT jobject JNICALL Java_com_test_SampleJNI_getID(JNIEnv *env, jclass cls) 
    {
      jclass c = (*jenv)->FindClass(env, "java/lang/Object");
      jmethodID m = (*jenv)->GetMethodID(env, c, "<init>", "()V");
      return (*env)->NewObject(env, c, m);
    }
    %}
    %native(getID) jobject getID();
    

    If you wanted to return something else (say java.lang.Integer) you would do something like:

    %module Sample
    %{
    JNIEXPORT jobject JNICALL Java_com_test_SampleJNI_getID(JNIEnv *env, jclass cls) 
    {
      jclass c = (*jenv)->FindClass(env, "java/lang/Integer");
      jmethodID m = (*jenv)->GetMethodID(env, c, "<init>", "()V");
      return (*env)->NewObject(env, c, m);
    }
    %}
    
    %typemap(jstype) int getID "Integer"
    %typemap(jtype) int getID "Integer"
    %native(getID) int getID();
    

    I specified getID explicitly in that typemap to avoid it being applied to all ints.

    (I tested the SWIG part of this is generating sensible code, but I didn’t verify the JNI and it clearly needs checking of return values)


    For your specific example you could do:

    %typemap(jstype) DeviceID getID "com.test.jni.DeviceId"
    %typemap(jtype) DeviceID getID "com.test.jni.DeviceId"
    %typemap(javaout) DeviceID getID { return $jnicall; }
    %native(getID) DeviceID getID();
    

    i.e. you can write literally anything name that isn’t already being used for something else and just apply a typemap.

    But it’s probably best to go with jobject as the return type as then you can just write:

    %typemap(jstype) jobject getID "com.test.jni.DeviceId"
    %typemap(jtype) jobject getID "com.test.jni.DeviceId"
    %native(getID) jobject getID();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.