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

  • SEARCH
  • Home
  • 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 6193415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:06:56+00:00 2026-05-24T03:06:56+00:00

I started using JNI to call Java classes from C++ some weeks ago and

  • 0

I started using JNI to call Java classes from C++ some weeks ago and today I encountered a peculiar situation. I am new to C++ (familiar with Java though), so this could be a n00b error. I have this class in Java called IntArray.java and I created another class in C++ called IntArrayProxy (split in a .h and a .cpp file) in order to access its methods through JNI. I also have another source file called IntArrayProxyTest.cpp which tests the IntArrayProxy methods.

In IntArrayProxy, I use a data member jobject* intArrayObject which contains the instance of the Java class and I pass this to every method of the IntArrayProxy class. My problem is that when I use it as a pointer (jobject*), after inserting (using insert) some integers and changing some of them (using setElement), when I use the same size() method twice, the executable I create crashes giving me a c0000005 exception (Access violation).

The first thing I noticed was that there is no problem at all if I use a normal jobject (not a jobject*) and the second one was that the exception occurs when I try to call a second non-void method. insert() and setElement(int, int) are both void, so I can call them as many times as I want. I tried it with almost all the non-void methods and the same exception was thrown each time I tried to call two non-void methods.

I thought that maybe the pointer somehow changed, so I tried printing the jobject* in each method but it stayed the same. The second explanation I found in forums was that maybe the object was destroyed but I don’t know how to check it and why this could happen. I spent all day searching and debugging but no luck.

I think it’s irrelevant but I am using the latest (32-bit) minGW compiler on Win7(64-bit). I also use the 32-bit jvm.dll. I am using the command line to compile it (g++ -I”C:\Program Files (x86)\Java\jdk1.6.0_26\include” -I”C:\Program Files (x86)\Java\jdk1.6.0_26\include\win32″ IntArrayProxy.cpp IntArrayProxyTest.cpp -L”C:\Users\jEOPARd\Desktop\Creta\JNI samples” -ljvm -o IntArrayProxyTest.exe)

Hope someone can help me!!

Thanx in advance!!

Kostis

IntArray.java

package SortIntArray;

public class IntArray {

private int[] arrayOfInt;
private int cursor;
private static final int CAPACITY = 5;

public IntArray() {
    arrayOfInt = new int[CAPACITY];
    cursor = 0;
}

public void insert(int n) {
    if (isFull()) {
        System.out.println("Inserting in a full array!");
    } else {
        arrayOfInt[cursor++] = n;
    }
}

public int removeLast() {
    if (isEmpty()) {
        System.out.println("Removing from an empty array!");
        return -666;
    } else {
        return arrayOfInt[--cursor];
    }
}

private boolean isEmpty() {
    return cursor <= 0;
}

private boolean isFull() {
    return cursor >= CAPACITY;
}

public String toString() {
    if (isEmpty()) {
        return "Empty Array";
    }
    String s = Integer.toString(arrayOfInt[0]);
    for (int i = 1; i < cursor; i++) {
        s += ", " + Integer.toString(arrayOfInt[i]);
    }
    return s;
}

public int size() {
    return cursor;
}

public int getElement(int pos) {
    return arrayOfInt[pos];
}

public void setElement(int pos, int newElement) {
    arrayOfInt[pos] = newElement;
}
}

IntArrayProxy.h

#ifndef INTARRAYPROXY_H
#define INTARRAYPROXY_H

#include <jni.h>
using namespace std;

class IntArrayProxy {
JNIEnv *env;
jclass intArrayClass;
jobject *intArrayObject; //giati oxi pointer?
public:

IntArrayProxy(JNIEnv*);

void insert(int n);
int removeLast();
string toString();
int size();
int getElement(int);
void setElement(int pos, int newElement);
jobject *getIntArrayObject();
};


#endif  /* INTARRAYPROXY_H */

IntArrayProxy.cpp

#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;

#include "IntArrayProxy.h"

IntArrayProxy::IntArrayProxy(JNIEnv *envir) {
env = envir;
intArrayClass = env -> FindClass("SortIntArray/IntArray");
if (intArrayClass == NULL) {
    cout << "--intArrayClass = NULL\n";
    exit(0);
}
jmethodID IntArrayConstructor = env->GetMethodID(intArrayClass, "<init>", "()V");
if (IntArrayConstructor == NULL) {
    cout << "--IntArrayConstructor = NULL";
    exit(0);
}
cout << "IntArrayProxy: Got constructor\n";
jobject obj = env -> NewObject(intArrayClass, IntArrayConstructor);
intArrayObject = &obj;     // I also can't assign intArrayObject directly at the above line, I don't know why (would be glad if you could tell me)
if (*intArrayObject == NULL) {
    cout << "--*intArrayObject = NULL";
    exit(0);
}
cout << "IntArrayProxy: Object created\n";
}

void IntArrayProxy::insert(int n) {
jmethodID insertID = env -> GetMethodID(intArrayClass, "insert", "(I)V");
if (insertID == NULL) {
    cout << "--insertID = NULL";
    exit(0);
}
env -> CallVoidMethod(*intArrayObject, insertID, (jint) n);
}

int IntArrayProxy::removeLast() {
jmethodID removeLastID = env -> GetMethodID(intArrayClass, "removeLast", "()I");
if (removeLastID == NULL) {
    cout << "--removeLastID = NULL";
    exit(0);
}
return (int) (env -> CallIntMethod(*intArrayObject, removeLastID));
}

string IntArrayProxy::toString() {
jmethodID toStringID = env -> GetMethodID(intArrayClass, "toString", "()Ljava/lang/String;");
if (toStringID == NULL) {
    cout << "--toStringID = NULL";
    exit(0);
}
jstring intArrayString = (jstring) env -> CallObjectMethod(*intArrayObject, toStringID);
string s = env -> GetStringUTFChars(intArrayString, NULL);
return s;
}

int IntArrayProxy::size(){
jmethodID sizeID = env -> GetMethodID(intArrayClass, "size", "()I");
if (sizeID == NULL) {
    cout << "--sizeID = NULL";
    exit(0);
}
return (int) (env -> CallIntMethod(*intArrayObject, sizeID));    
}

int IntArrayProxy::getElement(int pos) {
jmethodID getElementID = env -> GetMethodID(intArrayClass, "getElement", "(I)I");
if (getElementID == NULL) {
    cout << "--getElementID = NULL";
    exit(0);
}
return (int) env -> CallObjectMethod(*intArrayObject, getElementID, (jint) pos);
}

void IntArrayProxy::setElement(int pos, int newElement){
jmethodID setElementID = env -> GetMethodID(intArrayClass, "setElement", "(II)V");
if (setElementID == NULL) {
    cout << "--setElementID = NULL";
    exit(0);
}
env -> CallVoidMethod(*intArrayObject, setElementID, (jint) pos, (jint) newElement);    
}

jobject *IntArrayProxy::getIntArrayObject(){
return intArrayObject;
}

IntArrayProxyTest.cpp

#include <stdio.h>
#include <jni.h>
#include <cstdlib>
#include <iostream>
using namespace std;

#include "IntArrayProxy.h"

int main() {
cout << "--Starting..\n";
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=C:\\Users\\jEOPARd\\Desktop\\Creta\\JNI samples\\JNI tests\\build\\classes";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
 * pointer in env */
cout << "--Creating VM..\n";
JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
cout << "--VM created successfully!!\n";
delete options;

cout << "--Finding IntArray class..\n";
IntArrayProxy *intArrayProxy = new IntArrayProxy(env);
if (env->ExceptionOccurred())
    env->ExceptionDescribe();


intArrayProxy -> insert(1);
intArrayProxy -> insert(10);
intArrayProxy -> insert(3);
intArrayProxy -> insert(88);
intArrayProxy -> insert(32);

intArrayProxy ->setElement(2, 5);
intArrayProxy ->setElement(3, 7);    

cout << "Size: " << intArrayProxy -> size() << endl;
cout << "Size: " << intArrayProxy -> size() << endl;

cout << "--Destroying VM..\n";
jvm->DestroyJavaVM();
cout << "--Done!!!\n";
return 0;
}
  • 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-24T03:06:57+00:00Added an answer on May 24, 2026 at 3:06 am

    In the proxy constructor:

    intArrayObject = &obj;

    You’re taking the address of a variable on the stack. When the constructor exits, the address is no longer valid, hence the crash.

    The intArrayObject (in the header) should be a jobject, not a jobject*, and the various uses of it should be changed accordingly.

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

Sidebar

Related Questions

I started using symfony a few weeks ago, and there are some things that
I started using Sandcastle some time ago to generate a Documentation Website for one
We started using Mercurial a several weeks ago. Most developers follow this workflow: work
just started using JPA today, so i'm preety new to it. How would one
I started using Java a while ago so this is probably a silly question
Our customer is using our software (Java Swing application started using Webstart) besides other
Started using PDO prepared statements not too long ago, and, as i understand, it
I started using the Telerik Html.Grid today and have already run into a problem.
Just started using AJAX today via JQuery and I am getting nowhere. As an
I started using Prestashop yesterday so I’m a total beginner, but I've got some

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.