Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Jni Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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");
}

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
int a,b;
a=s.nextInt();
b=s.nextInt();
SumDemo sd=new SumDemo();
int c=sd.sum(a,b);
System.out.println("sum="+c);
// TODO code application logic here
}
private native int sum(int a,int b);
}
C Program:
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "sumheader.h"
JNIEXPORT jint JNICALL Java_sumdemo_SumDemo_sum
(JNIEnv *env, jobject job, jint a, jint b) {

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"

JNIEXPORT jstring JNICALL Java_stringdemo_StringDemo_stringcat


(JNIEnv *env, jobject job, jstring js) {
char *s;
jstring js2;
const jbyte *js1=(*env)->GetStringUTFChars(env,js,NULL);
s=malloc(strlen("Hello")+strlen(js1)+1);
strcpy(s,"Hello");
strcat(s,js1);
js2=(*env)->NewStringUTF(env,s);
(*env)->ReleaseStringUTFChars(env,js,js1);
free(s);
return js2;
}

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);
}

4. Sorting without returning the values


Java Program
package sortdemo;
import java.io.*;
import java.util.*;
public class SortDemo {

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]);

5. Sorting with returning values


Java Program
package sortingdeemo;
import java.io.*;
import java.util.*;
public class SortingDeemo {
static
{
System.load("D:\\javapro\\SortingDeeemo\\dist\\sorted.dll");
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int[] b=new int[20];
int[] c=new int[20];
for(int i=0;i<n;i++)
b[i]=s.nextInt();
SortingDeemo sd=new SortingDeemo();
c=sd.sorting(b,n);
// TODO code application logic here
for(int i=0;i<n;i++)
System.out.println("value of "+i+" is "+c[i]);
}
private native int[] sorting(int[] b,int n);
}

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;

public class CatchThrow {


static
{
System.load("D:\\javapro\\CatchC\\dist\\catch.dll");
}
private native void catchThrow() throws IllegalArgumentException;
private void callback() throws NullPointerException {
throw new NullPointerException("thrown in CatchThrow.callback");
}

public static void main(String[] args) {


CatchThrow c = new CatchThrow();
try {
c.catchThrow();
} catch (Exception e) {
System.out.println("In Java:\n " + e);
}
}
}
C Program
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "catchheader.h"

JNIEXPORT void JNICALL Java_catchthrow_CatchThrow_catchThrow


(JNIEnv *env, jobject obj)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "()V");
jthrowable exc;
if (mid == 0) {
return;
}
(*env)->CallVoidMethod(env, obj, mid);
exc = (*env)->ExceptionOccurred(env);
if (exc) {
/* We don't do much with the exception, except that we print a
debug message using ExceptionDescribe, clear it, and throw
a new exception. */
jclass newExcCls;

(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);

newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");


if (newExcCls == 0) { /* Unable to find the new exception class, give up. */
return;
}
(*env)->ThrowNew(env, newExcCls, "thrown from C code");
}
}

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");
}

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
int c;
ArithDemo ad=new ArithDemo();
try
{
c=ad.divide(a,b);
System.out.println("c="+c);
}
catch(Exception e)
{
c=0;
System.out.println("exception occurred"+e);

// TODO code application logic here


}
private native int divide(int a,int b);

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;
}
}

You might also like