http://jerome29antony.googlepages.com
• Type the java code and save it(say JNIPrac.java)
JNIPrac.java
************
import java.io.*;
class JNIPrac
{
public static native int addNumber(int x,int y);
static
{
System.loadLibrary("add");
}
public static void main(String args[])throws IOException
{
int x,y;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the 1'st number: ");
x=Integer.parseInt(br.readLine());
System.out.print("Enter the 2'nd number: ");
y=Integer.parseInt(br.readLine());
System.out.print("Sum of x and y is: "+addNumber(x,y));
}}
• Set the path and compile the java program
• Create the java header file using the command javah JNIPrac(if the class name is JNIPrac)
• Copy the contents of JNIPrac.h
• VC++->file->new->win32dynamic linked library->give project name as add and specify the location to save
• Go to fileview tab.File->new->C/C++ header file->specify a name(say add)->ok(it ll be store as add.h inside the header files folder in file view)
• File->new->C++ source file->specify a name(say add)->ok(it ll be store as add.cpp inside the source files folder in file view)
• Paste the contents of JNIPrac.h in add.h
add.h
*****
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h" /*This will be defined as #include
/* Header for class JNIPrac */
#ifndef _Included_JNIPrac
#define _Included_JNIPrac
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNIPrac
* Method: addNumber
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_JNIPrac_addNumber(JNIEnv *, jclass, jint, jint);/* copy this line and paste it in add.cpp exclude semicolon*/
#ifdef __cplusplus
}
#endif
#endif
add.cpp
*******
#include "add.h"
#include "jni.h"
JNIEXPORT jint JNICALL Java_JNIPrac_addNumber
(JNIEnv *env, jclass obj, jint x, jint y) /* specify members for the JNIEnv, jclass etc*/
{
return x+y;
}
• Go to c->jdk1.3->include->win32.Copy the two header files and paste in the project folder ie add
• Go to c->jdk1.3->include->copy jni.h and jvmdi.h and paste it in the project folder ie add
• Save the vc++ project
• Choose Build->Build add.dll
• Go to the add->debug->copy add.dll and paste it in the folder where the java file resides ie JNIPrac.java
• Run the java program
OUTPUT:
*********
D:\java_pgm\JNI>path=%path%;d:\jdk1.3\bin
D:\java_pgm\JNI>javac JNIPrac.java
D:\java_pgm\JNI>javah JNIPrac
D:\java_pgm\JNI>java JNIPrac
Enter the 1'st number: 15
Enter the 2'nd number: 25
Sum of x and y is: 40
CREATING A DLL IN VC++ AND USING IT IN JAVA
IMPLEMENTATION OF GENERIC HTTP SERVLET
PostParameterServlet.java
***********************
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParameterServlet extends GenericServlet
{
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException
{
PrintWriter pw=response.getWriter();
Enumeration eNum=request.getParameterNames();
while(eNum.hasMoreElements())
{
String pname=(String)eNum.nextElement();
pw.print(pname+"=");
String pvalue=request.getParameter(pname);
pw.println(pvalue);
}
pw.close();
}}
• Save the java file inside the folder called servlet
OUTPUT:
D:\java_pgm\servlet>set classpath=c:\jsdk2.0\lib\jsdk.jar
D:\java_pgm\servlet>path=%path%;c:\jdk1.3\bin\;c:\jsdk2.0\bin
D:\java_pgm\servlet>javac PostParameterServlet.java
D:\java_pgm\servlet>servletrunner -d c:\java_pgm\servlet -r c:\java_pgm\servlet
servletrunner starting with settings:
port = 8080
backlog = 50
max handlers = 100
timeout = 5000
servlet dir = D:\java_pgm\servlet
document dir = D:\java_pgm\servlet
servlet propfile = D:\java_pgm\servlet\servlet.properties
PostParameterServlet: init