Jni Programs
Jni Programs
Jni Programs
1. Arithmetic Operation
Java Program
package sumdemo;
import java.io.*;
import java.util.*;
public class SumDemo {
static
{
System.load("D:\\javapro\\addition\\dist\\summ.dll");
}
jint c;
c=a+b;
return c;
}
2. String Concat
Java Program
package stringdemo;
import java.io.*;
import java.util.*;
public class StringDemo {
static
{
System.load("D:\\javapro\\stringconcat\\dist\\string.dll");
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String a,b;
a=s.next();
StringDemo sd=new StringDemo();
b=sd.stringcat(a);
System.out.println("Resultant String="+b);
// TODO code application logic here
}
private native String stringcat(String a);
C Program
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include <string.h>
#include "stringheader.h"
3. Object Passing
Java Program
package objectpass;
import java.io.*;
import java.util.*;
public class ObjectPass {
private int number=100;
private String message="hi";
static
{
System.load("D:\\javapro\\ObjectC\\dist\\object.dll");
}
public static void main(String[] args) {
ObjectPass op=new ObjectPass();
op.modify();
System.out.println("Number In Java="+op.number+"\n"+"Text in
Java="+op.message);
// TODO code application logic here
}
private native void modify();
}
C Program
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "objectheader.h"
JNIEXPORT void JNICALL Java_objectpass_ObjectPass_modify
(JNIEnv *env, jobject job)
{
jclass tc=(*env)->GetObjectClass(env,job);
jfieldID fid=(*env)->GetFieldID(env,tc,"number","I");
jint num=(*env)->GetIntField(env,job,fid);
printf("Number in c=%d",num);
num=200;
(*env)->SetIntField(env,job,fid,num);
jfieldID fidm=(*env)->GetFieldID(env,tc,"message","Ljava/lang/String;");
jstring mess=(*env)->GetObjectField(env,job,fidm);
const char *str=(*env)->GetStringUTFChars(env,mess,NULL);
printf("\nMessage in C=%s\n",str);
mess=(*env)->NewStringUTF(env,"hello");
(*env)->SetObjectField(env,job,fidm,mess);
}
static
{
System.load("D:\\javapro\\Sortingdemo\\dist\\sort.dll");
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int[] b=new int[20];
for(int i=1;i<=n;i++)
b[i]=s.nextInt();
SortDemo sd=new SortDemo();
sd.sorting(b,n);
// TODO code application logic here
}
private native void sorting(int[] b,int n);
}
C Program
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "sortheader.h"
JNIEXPORT void JNICALL Java_sortdemo_SortDemo_sorting
(JNIEnv *env, jobject job, jintArray b, jint n) {
int i,j,temp;
jint *p=(*env)->GetIntArrayElements(env,b,NULL);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i]>p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
for(i=1;i<=n;i++)
printf("%d\n",p[i]);
C Program
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "soheader.h"
JNIEXPORT jintArray JNICALL Java_sortingdeemo_SortingDeemo_sorting
(JNIEnv *env, jobject job, jintArray b, jint n) {
int i,j,temp;
jint *p=(*env)->GetIntArrayElements(env,b,NULL);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i]>p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
jintArray array=(*env)->NewIntArray(env,n);
(*env)->SetIntArrayRegion(env,array,0,n,p);
return array;
6. Exceptions
Java Program
package catchthrow;
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
7. Arithmetic Exception
Java Program
package arithdemo;
import java.io.*;
import java.util.*;
public class ArithDemo {
static
{
System.load("D:\\javapro\\Excep\\dist\\except.dll");
}
C Program
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "arithheader.h"
JNIEXPORT jint JNICALL Java_arithdemo_ArithDemo_divide
(JNIEnv *env, jobject job, jint a, jint b) {
if(b==0)
{
jclass jx=(*env)->FindClass(env,"java/lang/ArithmeticException");
(*env)->ThrowNew(env,jx,"thrown form C code");
}
else
{
return a/b;
}
}