Error: Pointer type needed instead of JNIEnv_

T

TsanChung

How to fix the error in
ret = (*env)->NewStringUTF(env, newstring);
C++ on solaris 10?

# CC -Kpic -G -o libfoo.so -I/usr/java/include -I/usr/java/include/
solaris foo.cpp -z text
"foo.cpp", line 26: Error: Pointer type needed instead of JNIEnv_.
1 Error(s) detected.

foo.cpp:
#include <algorithm>
#include <jni.h>
#include "JNIFoo.h"

JNIEXPORT jstring JNICALL Java_JNIFoo_nativeFoo (JNIEnv *env, jobject
obj)
{
int i;
int ds_ret;

char* newstring;

jstring ret = 0;

newstring = (char*)new char[30];

if(newstring == NULL)
{
return ret;
}

std::fill(newstring, newstring + 30, 0);

newstring = (char *) "foo: Test program of JNI.\n";

ret = (*env)->NewStringUTF(env, newstring);

delete [] newstring;

return ret;
}


JNIFoo.java:
public class JNIFoo {
public native String nativeFoo();

static {
System.loadLibrary("foo");
}

public void print () {
String str = nativeFoo();
System.out.println(str);
}

public static void main(String[] args) {
(new JNIFoo()).print();
return;
}
}

# javac JNIFoo.java
# javah -jni JNIFoo
 
M

Mike Schilling

TsanChung said:
How to fix the error in
ret = (*env)->NewStringUTF(env, newstring);

env is a pointer; *env is not. So change it to

ret = env->NewStringUTF(env, newstring);
C++ on solaris 10?

# CC -Kpic -G -o libfoo.so -I/usr/java/include -I/usr/java/include/
solaris foo.cpp -z text
"foo.cpp", line 26: Error: Pointer type needed instead of JNIEnv_.
1 Error(s) detected.

foo.cpp:
#include <algorithm>
#include <jni.h>
#include "JNIFoo.h"

JNIEXPORT jstring JNICALL Java_JNIFoo_nativeFoo (JNIEnv *env,
jobject
obj)
{
int i;
int ds_ret;

char* newstring;

jstring ret = 0;

newstring = (char*)new char[30];

if(newstring == NULL)
{
return ret;
}

std::fill(newstring, newstring + 30, 0);

newstring = (char *) "foo: Test program of JNI.\n";

ret = (*env)->NewStringUTF(env, newstring);

delete [] newstring;

return ret;
}


JNIFoo.java:
public class JNIFoo {
public native String nativeFoo();

static {
System.loadLibrary("foo");
}

public void print () {
String str = nativeFoo();
System.out.println(str);
}

public static void main(String[] args) {
(new JNIFoo()).print();
return;
}
}

# javac JNIFoo.java
# javah -jni JNIFoo
 
T

TsanChung

The syntax


is the C calling convention to work with JNI. Since your source is in C++,  
you will need to use the C++ calling conventions.

Take a look athttp://java.sun.com/javase/6/docs/technotes/guides/jni/spec/design.ht...

HTH,

-Zig

Good advice.
Thanks.

extern "C" /* specify the C calling convention */
JNIEXPORT jstring JNICALL Java_JNIFoo_nativeFoo (JNIEnv *env, jobject
obj)
{
....
ret = env->NewStringUTF(newstring);
....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top