Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: How to call a C sort function to sort a Java Array.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to call a C sort function to sort a Java Array.

    My name is David, I'm interning this summer doing some High Performance Computing work. I'm significantly out of my comfort zone here; I am primarily a network/network security geek, not a programming guy. I took one Java based class called problem solving with programming where we wrote like 3 programs in Java and did everything else in pseudocode and using a program called Alice Alice.org to do things graphically. Learned basically no actual programming syntax. Also have done some self-taught perl, but only through one book and I didn't finish it, I only got about half way through it. So my expertise in programming are pretty much null.

    That being said, I currently am tasked with having to figure out how to make JNI work... specifically at this time I am tasked with writing an array in Java, and designing a C program that can be called by means of JNI to sort the array. I have chosen to work with the Merge Sort algorithm. My method of coding is not one where I write the entire thing from scratch, I don't particularly have a need to master languages at this point, rather I just need to make them work. I am interested in learning, but time is of the essence for me right now. So thus far what I have done is take sample codes and tweak them to meet my purpose. However, I currently am unable to make things work. So I am asking for help.

    I am going to paste 3 codes here, the first one will be my basic self-written instructions for JNI (Hello World Instructions), the second one will be my Java Array, and the third one will be my MergeSort function. I am not asking for you to DO my work for me by telling me how to manipulate my code, but rather I am asking for you to send me in the direction of resources that will be of some aid to me. Links, books (preferrably e-books so I don't have to go to a library), anything that you can send my direction that may help will be deeply appreciated. Thanks so much!

    JNI Instructions:
    /*The process for calling a C function in Java is as follows:
     
    1)Write the Java Program name. Eg. HelloWorld.java
    2)Compile it: javac HelloWorld.java
    3)Create a header file: javah -jni HelloWorld
    4)Create a C program eg. HelloWorld.java
    5)Compile the C program creating a shared library eg. libhello.so (My specifc command is cc -m32 -I/usr/java/jdk1.7.0_05/include -I/usr/java/jdk1.7.0_05/include/linux -shared -o libhello.so -fPIC HelloWorld.c 
    6) Copy the library to the java.library.path, or LD_LIBRARY_PATH (in my case I have set it to /usr/local/lib. 
    7)Run ldconfig (/sbin/ldconfig)
    8)Run the java program: java HelloWorld. */
     
    /*__________________________________________________________________________*/
     
    //Writing the code:
    //For the HelloWorld program: 
     
    //In java:
    //You need to name a class:
    class HelloWorld {
    //You then need to declare a native method:
    public native void displayHelloWorld();
    //You now need a static initializer:
    static {
    //Load the library:
    System.loadLibrary("hello");
    }
    /*Main function to call the native method (call the C code)*/
    public static void main(String[] args) {
    new HelloWorld().displayHelloWorld();
    }
    }
     
    //In C:
    #include <jni.h> //JNI header
    #include "HelloWorld.h" //Header created by the javah -jni command parameter
    #include <stdio.h> //Standard input/output header for C.
     
    //Now we must use a portion of the code provided by the JNI header.
    JNIEXPORT void JNICALL
    Java_HelloWorld_displayHelloWorld(JNIENV *env, jobject obj) 
    //Naming convention: Java_JavaProgramName_displayCProgramName
    {
        printf("Hello World!\n");
        return;
    }

    Java Array:
     class JavaArray {
         private native int MergeSort(int[] arr);
         public static void main(String[] args) 
         {
             int arr[] = {7, 8, 6, 3, 1, 19, 20, 13, 27, 4};
         }
         static 
         {
             System.loadLibrary("MergeSort");
         }   
     }

    Hacked and pieced together crappy C Merge Sort code:
    #include <jni.h>
    #include <stdio.h>
    #include "JavaArray.h"
     
     JNIEXPORT jint JNICALL 
     Java_JavaArray_MergeSort(JNIEnv *env, jobject obj, jintArray arr[],jint low,jint mid,jint high)
     {
       jint i,j,k,l,b[10];
     l=low;
     i=low;
     j=mid+1;
     while((l<=mid)&&(j<=high))
       {
        if(arr[l]<=arr[j])
          {
           b[i]=arr[l];
           l++;
          }
        else
          {
           b[i]=arr[j];
           j++;
          }
        i++;
       }
     if(l>mid)
       {
        for(k=j;k<=high;k++)
           {
            b[i]=arr[k];
            i++;
           }
       }
     else
       {
        for(k=l;k<=mid;k++)
           {
            b[i]=arr[k];
            i++;
           }
       }
     for(k=low;k<=high;k++)
        {
         arr[k]=b[k];
        }
    }
     
    void partition(jint arr[],jint low,jint high)
    {
     jint mid;
     if(low<high)
       {
        mid=(low+high)/2;
        partition(arr,low,mid);
        partition(arr,mid+1,high);
        sort(arr,low,mid,high);
       }
     
     }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 300 Times in 282 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Have you read this (not sure if it will help): Getting Started (Java Native Interface)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Dwere13 (July 11th, 2012)

  4. #3
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    This sort of looks like taking a medical student in his first week of medical school and asking him to read a manual on and then perform neurosurgery. Don't you need to know how to walk or even crawl before you try to fly?

  5. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Yeah I've read that, aussiemcgr, I actually found a link to that page in another thread on this forum. And it DID in fact help me significantly getting my Hello World stuff to work the other day. So thanks for throwing that out there, to you, and whomever else points people in that direction. It was very helpful, I just haven't found the solution to my issues in there currently. I'm getting the following error:
     cc -m32 -I/usr/java/jdk1.7.0_05/include -I/usr/java/jdk1.7.0_05/include/linux -shared -o libMergeSort.so -fPIC MergeSort.c
    MergeSort.c:7: error: conflicting types for ‘Java_JavaArray_MergeSort’
    JavaArray.h:16: error: previous declaration of ‘Java_JavaArray_MergeSort’ was here
    MergeSort.c: In function ‘Java_JavaArray_MergeSort’:
    MergeSort.c:16: warning: assignment makes integer from pointer without a cast
    MergeSort.c:21: warning: assignment makes integer from pointer without a cast
    MergeSort.c:30: warning: assignment makes integer from pointer without a cast
    MergeSort.c:38: warning: assignment makes integer from pointer without a cast
    MergeSort.c:44: warning: assignment makes pointer from integer without a cast

  6. #5
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Your method parameters are not matching. You need to show your JavaArray.h file, methinks.

  7. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Quote Originally Posted by Fubarable View Post
    Your method parameters are not matching. You need to show your JavaArray.h file, methinks.
    Well, mentioning the JavaArray.h file made me take a look at some things so I changed my Java code a tiny bit. I appreciate your comments.

    JavaArray.java now looks like:
     class JavaArray {
         private native int MergeSort(int[] arr, int low, int mid, int high);
         public static void main(String[] args) 
         {
             int arr[] = {7, 8, 6, 3, 1, 19, 20, 13, 27, 4};
         }
         static 
         {
             System.loadLibrary("MergeSort");
         }   
     }

    And my JavaArray.h file looks like:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class JavaArray */
     
    #ifndef _Included_JavaArray
    #define _Included_JavaArray
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     JavaArray
     * Method:    MergeSort
     * Signature: ([IIII)I
     */
    JNIEXPORT jint JNICALL Java_JavaArray_MergeSort
      (JNIEnv *, jobject, jintArray, jint, jint, jint);
     
    #ifdef __cplusplus
    }
    #endif
    #endif

  8. #7
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    That looks better, and looks to be in agreement with your c code. Does it work now?

  9. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Nope still got the same errors.

    Now, the C code I had posted had "jint low, jint mid, jint high" in it, and the java code has the same. However when I compile the Java code, and do javah -jni JavaArray the JavaArray.h file jsut says "jint, jint, jint". Now I know it says not to edit the .h file, but should I edit it so that it resmbles my C code more appropriately? Or is there something I'm missing in the Java Code that will get me the right .h file?

  10. #9
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    No, don't edit the .h file. Your .c file's method signature must match that of the .h file, and then call the c library's method using the parameters that have been passed in to it by Java. You probably should repost your .h file and your .c bridge file.

    Just to warn you, it's been several years since I've fiddled with JNI. I mainly use JNA (Java Native Access) instead since it is a *lot* easier to deal with. But it requires c code, or (I think) c++ code that is called with C calling conventions.

  11. The Following User Says Thank You to Fubarable For This Useful Post:

    Dwere13 (July 12th, 2012)

  12. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Sorry, my .c bridge file?

    My current .h file is:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class JavaArray */
     
    #ifndef _Included_JavaArray
    #define _Included_JavaArray
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     JavaArray
     * Method:    MergeSort
     * Signature: ([IIII)I
     */
    JNIEXPORT jint JNICALL Java_JavaArray_MergeSort
      (JNIEnv *, jobject, jintArray, jint, jint, jint);
     
    #ifdef __cplusplus
    }
    #endif
    #endif

    And my MergeSort.c file is:
    #include <jni.h>
    #include <stdio.h>
    #include "JavaArray.h"
     
     JNIEXPORT jint JNICALL Java_JavaArray_MergeSort
      (JNIEnv *, jobject, jintArray, jint low, jint mid, jint high)
     {
     jint i,j,k,l,b[10];
     l=low;
     i=low;
     j=mid+1;
     while((l<=mid)&&(j<=high))
       {
        if(arr[l]<=arr[j])
          {
           b[i]=arr[l];
           l++;
          }
        else
          {
           b[i]=arr[j];
           j++;
          }
        i++;
       }
     if(l>mid)
       {
        for(k=j;k<=high;k++)
           {
            b[i]=arr[k];
            i++;
           }
       }
     else
       {
        for(k=l;k<=mid;k++)
           {
            b[i]=arr[k];
            i++;
           }
       }
     for(k=low;k<=high;k++)
        {
         arr[k]=b[k];
        }
    }
     
    void partition(jint arr[],jint low,jint high)
    {
     jint mid;
     if(low<high)
       {
        mid=(low+high)/2;
        partition(arr,low,mid);
        partition(arr,mid+1,high);
        sort(arr,low,mid,high);
       }
     
     }

  13. #11
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Where's the .c file that you create that implements the function that is mapped out in the .h file? That's what I'm calling the "bridge" file. I'm not sure what the actual name is, but its name matches that of the .h file but it ends with .c or .cpp.

    I think that you may need to read this section in the JNI tutorials as it's well described there.
    Last edited by Fubarable; July 11th, 2012 at 06:21 PM.

  14. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Compiling in the manner that I do, the only other file created is my library file - libMergeSort.so (I'm using linux). And currently, that's not being created because my program will not compile under the current circumstances.

  15. #13
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    No, you've got to create the bridge C class. That's where all the work of JNI occurs. Again, read a detailed tutorial on this and you'll see.

  16. #14
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Read the instruction file that I posted in my original post.

    Specifically, these instructions. And for creating a HelloWorld program, and a couple of other simpler math programs (addition programs) this process worked properly. I created no additional bridge file.

    My process:
    /*The process for calling a C function in Java is as follows:
     
    1)Write the Java Program name. Eg. HelloWorld.java
    2)Compile it: javac HelloWorld.java
    3)Create a header file: javah -jni HelloWorld
    4)Create a C program eg. HelloWorld.java
    5)Compile the C program creating a shared library eg. libhello.so (My specifc command is cc -m32 -I/usr/java/jdk1.7.0_05/include -I/usr/java/jdk1.7.0_05/include/linux -shared -o libhello.so -fPIC HelloWorld.c 
    6) Copy the library to the java.library.path, or LD_LIBRARY_PATH (in my case I have set it to /usr/local/lib. 
    7)Run ldconfig (/sbin/ldconfig)
    8)Run the java program: java HelloWorld. */

  17. #15
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    It is step 4) create a C program eg. HelloWorld.java (it should be HelloWorld.c). You're not doing that.

  18. #16
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    That is MergeSort.c, and I've never had to create it with the same name as the java file. And my instructions was just a typo, I meant to write down .c not .java. Anyway, my MergeSort.C program is the "bridge" file.

    To show what I mean, I'll post a WORKING example with code, program names, and commands used:

    IntArray.java
     class IntArray {
         private native int sumArray(int[] arr);
         public static void main(String[] args) {
             IntArray p = new IntArray();
             int arr[] = new int[10];
             for (int i = 0; i < 10; i++) {
                 arr[i] = i;
             }
             int sum = p.sumArray(arr);
             System.out.println("sum = " + sum);
         }
         static {
             System.loadLibrary("IntArray");
         }
     }
    commands as follows:
    javac IntArray.java
    javah -jni IntArray

    sumArray.c
     #include <jni.h>
     #include <stdio.h>
     #include "IntArray.h"
     
     JNIEXPORT jint JNICALL 
     Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
     {
         jint buf[10];
         jint i, sum = 0;
         (*env)->GetIntArrayRegion(env, arr, 0, 10, buf);
         for (i = 0; i < 10; i++) {
             sum += buf[i];
         }
         return sum;
     }
    commands as follows:
    cc -m32 -I/usr/java/jdk1.7.0_05/include -I/usr/java/jdk1.7.0_05/include/linux -shared -0 libIntArray.so -fPIC sumArray.c
    cp libIntArray /usr/local/lib
    ldconfig
    java IntArray
    Output:
    [root@pe11196 Desktop]# java IntArray
    sum = 45
    [root@pe11196 Desktop]#

  19. #17
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,103
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: How to call a C sort function to sort a Java Array.

    specifically at this time I am tasked with writing an array in Java, and designing a C program that can be called by means of JNI to sort the array
    I know this comment doesn't directly address your immediate JNI needs, but here goes - sorting an array in java is easy - in fact, the algorithm is already written for you. I'd like to hope there is some benefit for using JNI that I've missed...

  20. #18
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Quote Originally Posted by copeg View Post
    I know this comment doesn't directly address your immediate JNI needs, but here goes - sorting an array in java is easy - in fact, the algorithm is already written for you. I'd like to hope there is some benefit for using JNI that I've missed...
    I know sorting an array in Java is easy. Because I'm unsure what level of clearance my actual project has, I can't fully explain anything to you. What I can say is this: There is an open source data management application that is used frequently for management of huge amounts of data in various fields. It is written entirely in Java. However, Java has limitations in networking, and in Memory access. In C, those limitations do not exist in the same way, so my mentor and his team have developed a back-end sort of application in C that they would like to communincate with the Java application. So, my mentor has got me researching JNI, and asked for me to find out how to sort a Java array with a C function.

    Thanks for your interest.

  21. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to call a C sort function to sort a Java Array.

    JNIEXPORT jint JNICALL Java_JavaArray_MergeSort
      (JNIEnv *, jobject, jintArray, jint low, jint mid, jint high)
    You're not properly declaring all your variables in the implementation file (.c file). Namely, the first 3 variables all need names.

    A second observation is that in your merge sort code you're not properly transferring the array into a C/C++ array. See your SumArray code for how to do that.

  22. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    Dwere13 (July 12th, 2012), Fubarable (July 12th, 2012)

  23. #20
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Thanks helloworld,

    So instead of what you quoted me saying, should it be:
    JNIEXPORT jint JNICALL Java_JavaArray_MergeSort
      (JNIEnv *env, jobject obj, jintArray arr, jint low, jint mid, jint high)
    And to properly transfer the array into the C array, is it this nifty little piece here? Or is there more to it:
        (*env)->GetIntArrayRegion(env, arr, 0, 10, buf);

    Thanks for your help.

  24. #21
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to call a C sort function to sort a Java Array.

    That's the basic jist of it. You need to ensure your buffer is large enough and that you're transferring over the correct portion of the array. You'll also need to transfer the array back.

  25. #22
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Quote Originally Posted by helloworld922 View Post
    That's the basic jist of it. You need to ensure your buffer is large enough and that you're transferring over the correct portion of the array. You'll also need to transfer the array back.
    Instead of GetIntArrayRegion could I do GetIntArrayElements? and also ReleaseIntArrayElements? and if not, to release it then would I do ReleaseIntArrayRegion?

  26. #23
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to call a C sort function to sort a Java Array.

    Quote Originally Posted by Fubarable View Post
    Just to warn you, it's been several years since I've fiddled with JNI. I mainly use JNA (Java Native Access) instead since it is a *lot* easier to deal with. But it requires c code, or (I think) c++ code that is called with C calling conventions.
    Think you could tell me a bit more about JNA? I just emailed my mentor about it, and researched it a bit on wikipedia. After my explanation about using a Java Front-end with a C back-end do you think JNA is a better option than JNI? Thanks so much for your help, and mentioning it. I appreciate your patience.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 22nd, 2011, 08:53 AM
  2. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 02:27 PM
  3. I Cant seem to get my sort routine to function
    By rrickman9 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 22nd, 2011, 07:48 PM
  4. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  5. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: December 1st, 2008, 08:02 AM

Tags for this Thread