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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:29:54+00:00 2026-05-25T17:29:54+00:00

I am using JNI to pass data between C++ and Java. I need to

  • 0

I am using JNI to pass data between C++ and Java. I need to pass a ‘long’ type, and am doing so using something like:

 long myLongVal = 100;
 jlong val = (jlong)myLongVal;
 CallStaticVoidMethod(myClass, "(J)V", (jvalue*)val);

However in Java, when the ‘long’ parameter is retrieved, it gets retrieved as some very large negative number. What am I doing wrong?

  • 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-05-25T17:29:55+00:00Added an answer on May 25, 2026 at 5:29 pm

    When you pass a jlong (which is 64 bit) as a pointer (which is, most likely, 32-bit) you necessarily lose data. I’m not sure what’s the convention, but try either this:

    CallStaticVoidMethodA(myClass, "(J)V", (jvalue*)&val); //Note address-of!
    

    or this:

    CallStaticVoidMethod(myClass, "(J)V", val); 
    

    It’s ...A methods that take a jvalue array, the no-postfix methods take C equivalents to scalar Java types.

    The first snippet is somewhat unsafe; a better, if more verbose, alternative would be:

    jvalue jv;
    jv.j = val;
    CallStaticVoidMethodA(myClass, "(J)V", &jv);
    

    On some exotic CPU archtectures, the alignment requirements for jlong variables and jvalue unions might be different. When you declare a union explicitly, the compiler takes care of that.

    Also note that C++ long datatype is often 32-bit. jlong is 64 bits, on 32-bit platforms the nonstandard C equivalent is long long or __int64.

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

Sidebar

Related Questions

I have written some code which passes values between java and c using jni.
Does Java pass by reference or value to C when using JNI ( more
When using JNI to interface between Java and C, javah parses a boolean value
I'm currently programming an interface between some C++ code and Java using JNI. I'm
Can I pass a string from Java to my C++ routine using JNI function
I am using JNI to call my C++ function from Java. One of the
I'm currently implementing a .NET wrapper for a Java library by using JNI to
I have a C program that stores some object in java store using JNI.
I'm trying to create a Qt main windows from Java using JNI directly and
Would it be possible to implement a custom Thread class in Java (using JNI)

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.