Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

jni oddness

Thread view: 
Ian Malone - 16 Apr 2007 12:13 GMT
Hi, I was wondering if anyone could suggest
reasons the following might be misbehaving.

I've followed the IBM JNI tutorial
<http://www-128.ibm.com/developerworks/edu/j-dw-javajni-i.html>.
IT works fine on Windows with jdk1.5.0_11
and Visual C++ 6.  I've also tried it on
Fedora Core 6, using gcc -shared to compile
the C code and Sun's jdk1.5.0_10.  Everything
seems to work except that the sum over the
integer array does not return the correct
result.  This is particularly bizarre because
setting the result individually to elements
of the array shows them to be correct.

Anything I could be trying to fix this behaviour?

Signature

imalone

Gordon Beaton - 16 Apr 2007 12:25 GMT
> Everything seems to work except that the sum over the integer array
> does not return the correct result. This is particularly bizarre
> because setting the result individually to elements of the array
> shows them to be correct.
>
> Anything I could be trying to fix this behaviour?

What is "it"?

We are not mindreaders. Without seeing your code it's virtually
impossible to see what the problem is.

The tutorial you refer to requires registration, so you need to
provide enough information in your post to make it possible for people
to help you.

/gordon

--
Ian Malone - 16 Apr 2007 12:41 GMT
>> Everything seems to work except that the sum over the integer array
>> does not return the correct result. This is particularly bizarre
[quoted text clipped - 11 lines]
> provide enough information in your post to make it possible for people
> to help you.

Sorry, as it's pretty much the only JNI tutorial
in existence I'd assumed it would be well known:

Sample1.java:

public class Sample1
{
  public native int intMethod(int n);
  public native boolean booleanMethod(boolean bool);
  public native String stringMethod(String text);
  public native int intArrayMethod(int[] intArray);

  public static void main(String[] args)
  {
    System.loadLibrary("Sample1");
    Sample1 sample = new Sample1();
    int     square = sample.intMethod(5);
    boolean bool   = sample.booleanMethod(true);
    String  text   = sample.stringMethod("JAVA");
    int     sum    = sample.intArrayMethod(
                        new int[]{1,1,2,3,5,8,13} );

    System.out.println("intMethod: " + square);
    System.out.println("booleanMethod: " + bool);
    System.out.println("stringMethod: " + text);
    System.out.println("intArrayMethod: " + sum);
  }
}

Sample1.c:

#include "Sample1.h"
#include <string.h>

JNIEXPORT jint JNICALL Java_Sample1_intMethod
  (JNIEnv *env, jobject obj, jint num) {
   return num * num;
}

JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
  (JNIEnv *env, jobject obj, jboolean boolean) {
  return !boolean;
}

JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
  (JNIEnv *env, jobject obj, jstring string) {
    const char *str = (*env)->GetStringUTFChars(env, string, 0);
    char cap[128];
    strcpy(cap, str);
    (*env)->ReleaseStringUTFChars(env, string, str);
    return (*env)->NewStringUTF(env, strupr(cap));
}

JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
  (JNIEnv *env, jobject obj, jintArray array) {
    int i, sum = 0;
    jsize len = (*env)->GetArrayLength(env, array);
    jint *body = (*env)->GetIntArrayElements(env, array, 0);
    for (i=0; i<len; i++)
    {    sum += body[i];
    }
    (*env)->ReleaseIntArrayElements(env, array, body, 0);
    return sum;
}

Sample1.h created by running javah on Sample1.java.
Though it occurs to me that I did this on the Windows
machine and copied the result to the Linux system;
is this a possible cause?  The problem is occuring
in the summation in Java_Sample1_intArrayMethod.

Signature

imalone

Gordon Beaton - 16 Apr 2007 13:12 GMT
> Sorry, as it's pretty much the only JNI tutorial in existence I'd
> assumed it would be well known:

Previously there was a JNI tutorial at java.sun.com. There's also the
JNI spec included in the JDK documentation package, and the Sheng
Liang book available online. I wasn't aware of the JNI tutorial at
IBM.

> Sample1.h created by running javah on Sample1.java. Though it occurs
> to me that I did this on the Windows machine and copied the result
> to the Linux system; is this a possible cause? The problem is
> occuring in the summation in Java_Sample1_intArrayMethod.

The code you posted works fine for me on Linux (I get 33).

This is how I compiled it with gcc:

 gcc -Wall -fPIC -I $JDK/include -I$JDK/include/linux -c Sample1.c
 gcc -shared Sample1.o -o libSample1.so

Try generating Sample1.h on the linux machine. If that doesn't help,
use printf() to display the value of len at the start of
intArrayMethod, and each body[i] value as you add them in the loop.

/gordon

--
Ian Malone - 16 Apr 2007 14:20 GMT
> Previously there was a JNI tutorial at java.sun.com. There's also the
> JNI spec included in the JDK documentation package, and the Sheng
> Liang book available online. I wasn't aware of the JNI tutorial at
> IBM.

Thanks, I hadn't realised the Liang book was online.

>> Sample1.h created by running javah on Sample1.java. Though it occurs
>> to me that I did this on the Windows machine and copied the result
>> to the Linux system; is this a possible cause? The problem is
>> occuring in the summation in Java_Sample1_intArrayMethod.
>
> The code you posted works fine for me on Linux (I get 33).

<bangs head on desk> Sorry, a misplaced semicolon had crept
in (I now notice it's in the copy on the Windows machine...).
Thanks for your help.

Signature

imalone

Gordon Beaton - 16 Apr 2007 14:27 GMT
> <bangs head on desk> Sorry, a misplaced semicolon had crept in (I
> now notice it's in the copy on the Windows machine...).

Yet another reason it's better to cut and past from real non-working
code when posting, rather than referring to a website that doesn't
actually have the error in it...

/gordon

--


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.