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 8180949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:21:54+00:00 2026-06-07T00:21:54+00:00

I am using Swig to create a Java interface. Basically, in C++ I have

  • 0

I am using Swig to create a Java interface. Basically, in C++ I have template (

template<class T> class TSizedArray
{
    int GetLength();
    T* GetElements() const;
    ...
}

class Producer
{
    TSizedArray<long> GetLongs();
    ...
}

This template is used as return value of some functions of API I need to export to Java via SWIG. However, in Java we want to use native Java arrays instead of this simple wrapper:

Java:
    long[] result = Producer.GetLongs();

I am not sure how to achieve this with SWIG. The template has multiple instantiations, and is used as input and output of multiple methods in different classes. I was able to use:

%typemap(jstype) TCustomElementList "long[]"

to get the generated Java interface as long[] GetLongs(), but the generated implementation creates new object of type TCustomElementList.

Thanks for any ideas.

  • 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-07T00:21:55+00:00Added an answer on June 7, 2026 at 12:21 am

    You’re on the right lines with your typemap, but you need a few more than just that one:

    %module test
    
    template<class T> class TSizedArray
    {
    public:
        int GetLength();
        T* GetElements() const;
    };
    
    %typemap(jstype) TSizedArray<long> "long[]"
    %typemap(jtype) TSizedArray<long> "long[]"
    %typemap(javaout) TSizedArray<long> {
        return $jnicall;
      }
    
    %typemap(out) TSizedArray<long> {
      $result = JCALL1(NewLongArray, jenv, $1->GetLength());
      JCALL4(SetLongArrayRegion, jenv, $result, 0, $1->GetLength(), $1->GetElements());
    }
    
    class Producer
    {
    public:
        TSizedArray<long> GetLongs();
    };
    

    (This interface ran through SWIG fine, but hasn’t been tested further).

    You need to set the return types for both the Proxy class and the JNI class (jtype and jstype typemaps). You also need to instruct the proxy to pass through what the JNI code returns (javaout typemap). Then you need provide a bit of JNI glue code that gets inserted to make a new Java array in the C++ side (out typemap).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Swig to create a Java wrapper for a a library
I am trying to wrap a C++ class using SWIG to create a Java
I'm using SWIG to create a Python interface to my C++ class library. I
OK so I have a C++ class that is exposed to Lua using SWIG.
I'm generating code in java using SWIG. One of the generated class belongs to
Has anyone had luck using CMake to create Ruby bindings via SWIG? I have
I'm using SWIG to export a c++ class to Java, but ran into a
I want to start learning more about using SWIG and other methods to interface
I'm using SWIG 2.0 to create a Python wrapper for a C++ library. One
I just on internet the Google is using swig. Does any one have used

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.