Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

948886Jul 11 2012 — edited Jul 11 2012
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 http://www.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=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);
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2012
Added on Jul 11 2012
1 comment
2,097 views