• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JNI What is it, and can I use it

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering what JNI was and if I could use it to convert my C++ program that I have written with Borland C++ Builder 5.0 into java. I would appricate any help, Thanks!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JNI (Java Native Interface) will let you use your C code WITHOUT converting it to JAVA. You just blend them together using this interface.
Here is a tutorial: http://java.sun.com/docs/books/tutorial/native1.1/index.html
 
Terry Stehle
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you help
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example of How to use JNI to let Java Call C
Including Java Send Para (no pointer) into C and Java Get Value returned from C function
[Step 1]
>Create HelloWorld.java
-------------------------------------------------------
class HelloWorld {
public native void displayHelloWorld(int mint);
public native int ReturnValue(int mint);
static {
System.loadLibrary("hello");
}

public static void main(String[] args) {
new HelloWorld().displayHelloWorld(123);
int i=new HelloWorld().ReturnValue(123);
System.out.println(i);
}
}
-------------------------------------------------------
[Step 2]
>Command Line Type
d:\jni\javac HelloWorld.java
-------------------------------------------------------
[Step 3]
>Command Line Type
d:\jni\javah HelloWorld
HelloWorld.h would be automaticly Created
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: ReturnValue
* Signature: (I)I
*/

JNIEXPORT jint JNICALL Java_HelloWorld_ReturnValue
(JNIEnv *, jobject, jint);
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
-------------------------------------------------------
[Step 4]
>Write HelloWorldImp.c file
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj,jint mint)
{
printf("My Hello world %d!\n",mint);
return;
}
JNIEXPORT jint JNICALL Java_HelloWorld_ReturnValue(JNIEnv *env, jobject obj,jint mint)
{
return mint+1;
}
-------------------------------------------------------
[Step 5]
>Make hello.dll Using VC commandline
cl -I c:\jdk1.3\include -I c:\jdk1.3\include\win32 -LD HelloWorldImp.c -FeHello.dll
Message Displayed
/dll
/implib:Hello.lib
/out:Hello.dll
HelloWorldImp.obj
Creating library Hello.lib and object Hello.exp
-------------------------------------------------------
[Step 6]
> Run HelloWorld.class
Message Displayed
My Hello world 123!
124
 
Are you here to take over the surface world? Because this tiny ad will stop you!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic