About JNI

Hi,
I am new to JNI programming. I tested the following hello world programm:
HelloWorld.java:
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary("HelloWorld");
public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
HelloWorld.cpp:
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
printf("Hello world!\n");
return;
I compiled the C++ code with Visual C++ 6.0, I created de project with the opcion "MFC AppWizard (dll)"
and when I compiled the code with C++ I have an error "fatal error C1083: cannot open include file: �jni.h�: no such file or directory
error executing cl.exe"
I mean the c++ code was not compiled and when I did java HelloWord I had the follow error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: displayHelloWord
     at HelloWorld().displayHelloWorld(Native Method)
     at HelloWorld().main(HelloWorld.java)
I have no idea what this mean
Anyone have an idea? please help me
Thank you in advance.

Hi
I could compile the file C++ and I tried to run the file HolaMundo.java but I have the follow error
C:\Documents and Settings\yamilet\Mis documentos\prueba\intento>java -Djava.libr
ary.path=. HolaMundo
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HolaMundo in java.
library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at HolaMundo.<clinit>(HolaMundo.java:9)
C:\Documents and Settings\yamilet\Mis documentos\prueba\intento>
I have the files HolaMundo.h and HolaMundo.java inside the directory Hola.
The directory Hola had the C++'project and the file HolaMundo.dll was inside the Debuc'folder
and Debuc'folder was automatic generate for the C++'compiler.
I mean The loadLibrary isn't work, I mean the java'compiler don't know where is the file HolaMundo
I tried to put it inside the directory Debug but I still the error.
I tried to put it inside the directory hola but I still the error.
The file HolaMundo.java is
public class HolaMundo
     private native void saluda();
     public static void main(String[] args)
     new HolaMundo().saluda();
     static
     System.loadLibrary("HolaMundo");
HolaMundo is the file .dll and it is inside the directory Debug
Have you any idea? Please help me
Thank for advance.

Similar Messages

  • About JNI.***.dll already loaded in another class loader.

    About JNI.***.dll already loaded in another class loader.
    When I run a WEB program that use's JNI and DLL.The following excetpion information is throwed:
    .***.dll already loaded in another class loader.(string "***" represents a dll name).
    Help! Who can provide a solution.
    Thanks.

    i got the same problem before. seems it happens when
    you load the .dll not using static{ } block. And when
    your web application got an error and you fix it to
    restart your application without restarting your
    server, you will get this error message. You can
    either put load library stuff in the static{ } block
    or restart your server.And again.......
    If all you want is to have the dll loaded, and it is loaded (that is why the exception occurs) then it does not matter that the exception occurs. You can catch it and ignore it.

  • Information about jni.h

    Hi,
    I want to know about the JNI functions that are being used in the native code , eg c or c++ .
    Take the example of the function GetObjectClass. Now this has been defined in jni.h as
    jclass GetObjectClass(jobject obj) {
    return functions->GetObjectClass(this,obj);
    where functions is defined in struct JavaVM_ as
    const struct JNIInvokeInterface_ *functions;
    Now when I look into struct JNIInvokeInterface_ there is no mention of the GetObjectClass. Then where does this function come from. Is it here in libjvm.so or what?
    Please explain me the same.
    Thanks and Regards,
    Anand

    In general I would say that the JNI functions "bottom out" in some native code inside the JVM. That native code may, in fact, ultimately call up to something in a jar, but it may not.
    Regarding your example: one way you could check this is to take a look at the java source code, and find out if Object.getClass is entirely implemented in java, or calls a native implementation function.

  • A question about JNI references, persistence, and garbage collection

    I am writing a library to allow our Java developers to access C functions in our product. I want people on the Java side to be able to instantiate instances of a new object (on the C side), say "Map", and I want those objects to persist until they are destroyed on the Java side. I am thinking of creating a wrapper for the native methods, which stores an identifier for the instance of an object. When the C object is accessed, the identifier is passed to the C code which looks up the correct object in a table. I understand that if I use the NewGlobalReference call in JNI I can get objects to persist.
    When it is time for garbage collection I plan to override finalize() to call the C code and tell it to remove the Global Reference.
    Here's what I have in mind
    public class JMap() {
         private static int id;
         static {
              System.loadLibrary("libMap");
              /*Call C method which instantiates object and creates a GlobalReference
              which is stored in table. Returns ID for reference*/
              id = _init();
         public void setSize(int x, int y) {
              _setSize(id, x, y);
         public void finalize() {
              _finalize(id);
              super.finalize();
         private native int _init();
         /*calls DeleteGlobalReference for the reference matching this id*/
         private native void _finalize(int id);
         private native void _setSize(int id, int x, int y);
    }Is this the right thing to do? Have I understood the way JNI works with regard to GlobalReferences?
    Any comments, tips or pointers would be appreciated :)

    This probably should have been posted in the Native Methods sub-forum, I have reposted there:
    http://forum.java.sun.com/thread.jspa?threadID=657667
    Mods please delete this post :)

  • Doubts About JNI

    Hello,
    I have successfully run many JNI programs... but all have been console based.. i am thinking that can i make my GUI in vb6 or vc++ 6.0 and then use the dll file and run it. call my gui through the java program...
    But i have been told that i can't use the dll file build in VB 6.0 and the DLL file Built in .net ..... so i have this program written in VB 6 and it uses the winmm.dll and i am interested in opening the CD-ROM thorugh the java program....
        Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpCommandString As String, ByVal lpReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim retval As Long
            retval = mciSendString("set CDAudio door open", "", 0, 0)
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim retval As Long
            retval = mciSendString("set CDAudio door closed", "", 0, 0)
        End Subnow if i want to open the CD-ROM through java i must convert it into VC++ and make a function then call it through my java program...
    Guyz am i right ??

    The code you are showing won't even compile!
    There are many errors in it.
    So with what you are showing there is no way that you have built a DLL,
    even less chance that you have executed some test that lead to an unsatisfied link error.
    Assuming that you have somewhere the actual code that compiled to a DLL,
    is your Java class with the native methods in it "in sync" with that DLL?
    By "in sync" I mean does the methods name, return type and parameters type
    and also the DLL name used when calling System.loadLibrary(), reflect the built DLL?
    For instance with what you've shown and assuming that you have built the DLL HelloWorld.dll,
    the Java class should look like:
    public class HelloWorld {
        static {
            System.loadLibrary("HelloWorld");
        public native void opencd();
        public native void closecd();
    }Does it?
    Also are you executing your test in the same folder where are located the Java class(es) and the DLL?
    Also which "unsatisfied link error" are you having?
    Don't translate it for us in your post, rather copy and paste the actual error message in the post..

  • General purpose topic about JNI

    hi,
    i just the read the JNI Tutorial, and i am wondering if i have well understood.
    As explained in the tutorial, JNI can be used to call C/C++ functions from inside a java class, but tell me if i am wrong : do have i have to rename all my C functions in order to accord the naming standard (java_classname_methodname) ?
    I mean, i have a C file with a "somestruct* getInfo(const char * fileName)" function
    do i have to rename the method to accord the standard or should i create another C file which will call the first one ?
    My aim is to have my Java program access some method from a pre-existing C library.
    Yours,
    Metin

    Create another function to call the first one. You are not going to be able to return structs anyhow. The "wrapper" is the place to do the adjustments.

  • Problem based on old post about JNI sysHook.c,PollThread,KeyboardHook...

    Hello nice people,
    I am having a hard time solving my boring problem related to JNI. Please consider the exception of this PollThread class:
    //PollThread.java
    package sysHookJNI;
    import java.io.*;
    public class PollThread extends Thread
         public native void checkKeyboardChanges();
         private KeyboardHook kbh;
         public PollThread( KeyboardHook kh )
              kbh = kh;
              System.loadLibrary("syshook");
         public void run()
              for(;;)
                   checkKeyboardChanges();   //  <--- this is line 21.
                   yield();
         void Callback( boolean ts, int vk, boolean ap, boolean ek )
              KeyboardEvent event = new KeyboardEvent( this, ts, vk, ap, ek );
              if( ts )
                   kbh.keyPressed( event );
              else
                   kbh.keyReleased( event );
    }* Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: sysHookJNI.PollThread.checkKeyboardChanges()V*
    *     at sysHookJNI.PollThread.checkKeyboardChanges(Native Method)*
    *     at sysHookJNI.PollThread.run(PollThread.java:21)*
    I tried so many possible ways to solve above problem either by regenerating my header file and every related classes: KeyboardEvent, KeyboardEventListener, KeyboardHook, PollThread, Test. Then I run Test which contains main() then stuck at PollThread class. In fact, it works perfectly w/o locating in a package, but I need to put it into a package to work with my java project.
    Please help me out with your suggestion!! It has been a week searching for the solution..
    Edited by: Nethie on Nov 1, 2009 3:06 PM

    Nethie wrote:
    In fact, it works perfectly w/o locating in a package, but I need to put it into a package to work with my java project. When means that you did not in fact redo the signature. If the package changes the signature changes which means
    1. The generated h file changes
    2. Your implementation of the signature must change.

  • How to get MS Access report into Java by JNI

    hi,
    I am new to JNI and I need to know how could I get MS Access report data in my application. Specially, I am having difficulties in C coding part. I read API documentation and tutorials about JNI, so if you know where could I read about this topic send me that too.
    thanks

    use j-Interop , it has a sample for ADO access.

  • SAX and JNI

    I'm having problems using Java classes from C. I've managed to trace the problem as far as the SAX parser, but I have no idea why there's a problem at all.
    With the -verbose:jni option on, there's a message: "Unable to read from file" and then the JVM stops (crashes actually.)
    I create a VM as follows:
            JavaVM* jvm;
         JNIEnv* env;
         JavaVMInitArgs args;
         JavaVMOption options[3];
         int ret;
         args.version = JNI_VERSION_1_4;
         args.nOptions = 3;
         options[0].optionString = classPath;
         options[1].optionString = "-verbose:jni";
         options[2].optionString = "-Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl";
         args.options = options;
         args.ignoreUnrecognized = JNI_TRUE;
         if ((ret = JNI_CreateJavaVM(&jvm, (void **)&env, &args)) < 0) {
                /* error */
            }classPath is just a char* that lists all the Jars the project uses. I wrote a Java main() to test my code and when you run it with the exact same classpath, it works.
    The Java code that creates and starts the SAX parser looks like:
         XMLHandler handler = new XMLHandler ();
         SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            try {
                 saxParser.parse(f, handler);
            } catch (java.io.IOException e) {
                 e.printStackTrace ();
            } catch (Exception e) {
                 e.printStackTrace ();
            }Is there something about JNI that SAX stuff won't work with it?
    Any help is greatly appreciated.

    D'oh!
    The java test code that "worked" actually used the wrong file name so the file didn't exist so it didn't actually execute the broken code.
    When I hard-code the absolute path to the file, I still get "Unable to read from file". So I guess the problem isn't with the JNI but somewhere else in my code.
    Thanks.

  • Need Help .... (JNI)

    Dear
    Well, i try to learn about JNI so i try stegostick, u can get stegostick at http://sourceforge.net/projects/stegostick
    i can understand this application until this problem appear T_T
    Java interface and C++ library have done and i try to call library(StegBMP.dll) and i got this error T_T
    Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Java.Hide.hideInBMP(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
    at Java.Hide.hideInBMP(Native Method)
    if someone have free time, pls try this application (http://sourceforge.net/projects/stegostick) and i hope can solve this problem XD........
    My english not really good, so i hope u understand
    Regrads
    Shinlei

    and it's from C++
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include"EasyBMP.h"
    #include"Hide.h"
    #include"Unhide.h"
    using namespace std;
    char ext[4];              // extention of encrypted file i.e txt, png etc
    unsigned int size;        // size of output file
    char sizeInChar[4];       // size of output file in characters
    unsigned int height;      // height of image
    unsigned int width;       // width  of image
    char verification[] = {'s','t','e','g','o','\0'};
    char verify[6];
    BMP Image;                // image pointing to cover image
    char stegoFile[50];
    char *message;
    char *outputFile;
    const char *pwd;
    int pwdlen;
    char extractChar(int i, int j){    
         RGBApixel pixel1 = *Image(i,j);
         i++;
         if( i == Image.TellWidth() ){ i=0; j++; }      
         RGBApixel pixel2 = *Image(i,j);   
         unsigned int T = 0;         
         T = T | pixel1.Red % 2;
         T = T << 1;
         T = T | pixel1.Green % 2;
         T = T << 1;
         T = T | pixel1.Blue  % 2;
         T = T << 1;
         T = T | pixel1.Alpha  % 2;
         T = T << 1;    
         T = T | pixel2.Red  % 2;
         T = T << 1;
         T = T | pixel2.Green % 2;
         T = T << 1;
         T = T | pixel2.Blue % 2;
         T = T << 1;
         T = T | pixel2.Alpha % 2;
         char c = (char) T;    
         return c;
    void embedChar(char c,int i, int j){
        RGBApixel pixel1, pixel2; // pixels having one character   
         // Get Two Pixels to embed
         int ti=i, tj=j;    
         pixel1 = *Image(i,j);
         i++;
         if( i == width ){ i=0; j++; }              
         pixel2 = *Image(i,j);             
         unsigned short int mask = 0x80;  
         if( c & mask )
             pixel1.Red = pixel1.Red | 1;       
         else
             pixel1.Red = pixel1.Red & 0xfe;
         mask = mask >> 1;        
         if( c & mask )
             pixel1.Green = pixel1.Green | 1;
         else
             pixel1.Green = pixel1.Green & 0xfe;      
         mask = mask >> 1;        
         if( c & mask )
             pixel1.Blue = pixel1.Blue | 1;
         else
             pixel1.Blue = pixel1.Blue & 0xfe;  
          mask = mask >> 1;   
          if( c & mask )
             pixel1.Alpha = pixel1.Alpha | 1;  
         else
             pixel1.Alpha = pixel1.Alpha & 0xfe;       
         mask = mask >> 1;   
         if( c & mask )
             pixel2.Red = pixel2.Red | 1; 
         else
             pixel2.Red = pixel2.Red & 0xfe; 
         mask = mask >> 1;   
         if( c & mask )
             pixel2.Green = pixel2.Green | 1;  
         else
             pixel2.Green = pixel2.Green & 0xfe; 
         mask = mask >> 1;   
         if( c & mask )
             pixel2.Blue = pixel2.Blue | 1;    
         else
             pixel2.Blue = pixel2.Blue & 0xfe;   
         mask = mask >> 1;   
         if( c & mask )
             pixel2.Alpha = pixel2.Alpha | 1;   
         else
             pixel2.Alpha = pixel2.Alpha & 0xfe;       
         *Image(ti,tj) = pixel1;               
         *Image(i,j)   = pixel2;             
    int hasExtention(const char *f){
         for(int i=0; i<strlen(f); i++){
              if(f=='.')
                   return 1;
         return 0;
    JNIEXPORT jint JNICALL Java_Hide_hideInBMP
    (JNIEnv *env, jclass jc, jstring secretFileName, jstring coverFileName, jstring dstPath, jstring password){         
    const char* hiddenFile;
    const char* coverFile;
    const char* stegoFile;
    const char* dstPathName;
    const char* msg;
    char c;
    int i=0, j=0;
    FILE* fp;
    jboolean tr = (char)1;
    hiddenFile = env->GetStringUTFChars( secretFileName, &tr );
    coverFile = env->GetStringUTFChars( coverFileName, &tr );
    dstPathName = env->GetStringUTFChars( dstPath, &tr);
    pwd = env->GetStringUTFChars(password,&tr);
    pwdlen = strlen(pwd);
    cout<<"Entered";                     
              if(hasExtention(hiddenFile)){
                        char *q = strchr(hiddenFile, '.');                    
                        ext[0] = ((*(q+1) != '\0') ? *(q+1) : 0) ;
                        ext[1] = ((*(q+2) != '\0') ? *(q+2) : 0) ;
                        ext[2] = ((*(q+3) != '\0') ? *(q+3) : 0) ;
                        ext[3] = ((*(q+4) != '\0') ? *(q+4) : 0) ;
              else{
                   ext[0] = 'y';
                   ext[1] = 'y';
                   ext[2] = 'y';
                   ext[3] = 'y';
    fp = fopen( hiddenFile , "rb" );
    if( !fp ){
    cout << "Error: unable to read file " << hiddenFile << " for text input!" << endl;
    return 1;
    size = 0;
    while( !feof( fp ) ){
    char c;
    fread( &c, 1, 1, fp );
    size++;
    size--;
    fseek(fp,0,SEEK_SET);
    if(!(Image.ReadFromFile( coverFile )))
    return 2;     
    width = Image.TellWidth();
    height = Image.TellHeight();
    int MaxNumberOfPixels = width * height - 2;
    Image.SetBitDepth( 32 );
    if( 2 * size+16+10 > MaxNumberOfPixels ){
    cout<<"Error : hidden file exceeds encoding capacity of the image\n";
    return 3;
    unsigned int tempSize = size;
    sizeInChar[3] = (unsigned char) (tempSize & 0x000000ff);
    tempSize = tempSize>>8;
    sizeInChar[2] = (unsigned char)(tempSize & 0x000000ff) ;
    tempSize = tempSize>>8;
    sizeInChar[1] = (unsigned char)(tempSize & 0x000000ff);
    tempSize = tempSize>>8;
    sizeInChar[0] = (unsigned char)(tempSize & 0x000000ff);
    srand(15243);
    i = 0;
    j = 0;
         int p;
    for( p=0; p<5; p++){
    c = verification[p];
                   c = c ^ pwd[rand()%pwdlen];
    embedChar(c,i,j);
    i++;
    if(i == width) { i = 0; j++; }
    i++;
    if(i == width) { i = 0; j++; }
    for( p=0; p<4; p++){
    c = ext[p];
    embedChar(c,i,j);
    i++;
    if(i == width) { i = 0; j++; }
    i++;
    if(i == width) { i = 0; j++; }
    for(p=0; p<4; p++){
    c = sizeInChar[p];
    embedChar(c,i,j);
    i++;
    if(i == width) { i = 0; j++; }
    i++;
    if(i == width) { i = 0; j++; }
    for( p=0; p<size; p++){             
    fread( &c, 1, 1, fp );
    embedChar(c,i,j);
    i++;
    if(i == width) { i = 0; j++; }
    i++;
    if(i == width) { i = 0; j++; }
    char outputFile[50];
    strcpy(outputFile, dstPathName);
    strcat(outputFile, "/steg.bmp" );
    if( ! (Image.WriteToFile(outputFile)))
    return 5;
    cout<<"Hidden File is successfully Encoded into cover Image and outputed to steg.bmp";
    return 4;

  • How to use JNI?

    hi all,
    i know that pure java can't disable alt+tab,i must use JNI with C language. i have a pile of codes to disable the alt+tab in C but i do not know how to do it in JNI...can anyone help?thanks in advance...
    these are my codes in C:
    Private Declare Function SystemParametersInfo Lib "user32" _
    Alias "SystemParametersInfoA" (ByVal uAction As Long, _
    ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long
    Private Const SPI_SCREENSAVERRUNNING = 97
    Dim ret As Integer
    Dim pOld As Boolean
    Private Sub Form_Load()
    'The line below calls the function and passes
    'the constant and TRUE to fool Windows into thinking
    'the screensaver is running and FALSE to do the opposite...
    ret = SystemParametersInfo(SPI_SCREENSAVERRUNNING, True, pOld, 0)
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    'NOTE: THIS MUST BE SET TO FALSE WHEN
    'THE FORM OR APP CLOSES
    'If you do not set it to false, these keys will not work
    'with any app, or with anything in the Windows Environment...
    ret = SystemParametersInfo(SPI_SCREENSAVERRUNNING, False, pOld, 0)
    End Sub

    There really isn't a lot to using JNI.
    There are examples on Sun's website:
    http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html
    There is also a little more simple example at:
    http://www.inonit.com/cygwin/jni/helloWorld/java.html
    Basically, here are the steps:
    1) Create a simple java applet that will call your C/C++ code.
    For practise, I would just use the HelloWorld example from www.inonit.com:
    package example.jni;
    public class HelloWorld {
         private static native void writeHelloWorldToStdout();
         public static void main(String[] args) {
              writeHelloWorldToStdout();
    2) Name the file with the code above HelloWorld.java and compile it:
    javac HelloWorld.java
    3) Use javah to generate the C header file:
    javah example.jni.HelloWorld
    (You may need to cd to the parent directory of 'example', in this case)
    4) The above line should create a example_jni_HelloWorld.h header file.
    Use this header file in your C/C++ DLL. Make sure your C/C++ compile knows where to find jni.h. In my compiler, I added the following paths:
    C:\J2SDK1.4.1\INCLUDE\
    C:\J2SDK1.4.1\INCLUDE\WIN32
    5) The trickiest part about JNI is passing in and returning parameters.
    If you don't have to pass in or return any parameters, then you're almost done. Replace your C/C++ function protocol with the protocol in the header file you generated. Compile your DLL.
    6) Copy the DLL to a path that it will be found by your Java Applet. (If running on Windows, this would be your System32 or System directory.)
    7) I also had to edit my Path environment variable to include the Java Runtime bin directory. On my machine it was: C:\J2SDK1.4.1\jre\bin
    8) Try running your java applet:
    Java example.jni.HelloWorld
    Good Luck!

  • Is it possible to call a non-JNI DLL

    I have a DLL I'd like to execute from Java, but do not have access to the source. From what I've read about JNI, it seems it requires Java specific code in the DLL source. Is there any other option? I need to pass it a String and it returns a String. Is Runtime.exec a viable option?
    Any advice would be greatly appreciated.

    OK... here is a scenario with some code snippets. I've done this with a C/C++ dll on windows....so if ur on some other OS you'll have to make appropriate changes(ditto for DELPHI).
    I have a dll called "myold.dll" written in C/C++ which has the following function signature
    int getVersion();
    My purpose is to call this function in the dll using JNI.
    For this you will need
    1. Header file of myold.dll (say myold.h)
    2. .lib file for myold.dll (say myold.lib)
    Now, write a java file "Mynative.java"
    public class Mynative {
    public native int getVersion();
       static {
          System.loadLibrary("Mynative"); //matches name of  new dll you are about to create
       public static void main(String[] args){
          Mynative nativity = new Mynative();
          int version = nativity.getVersion();
          System.out.println(version);
    }after compiling the above code, you will need to run javah
    on the resulting class file
    javah -jni Mynative
    Now you will have a header file Mynative.h which looks like
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class Mynative */
    #ifndef IncludedMynative
    #define IncludedMynative
    #ifdef __cplusplus
    extern "C" {
    #endif
    * Class: Mynative
    * Method: getVersion
    * Signature: ()I
    JNIEXPORT jint JNICALL Java_Mynative_getVersion
    (JNIEnv *, jobject);
    #ifdef __cplusplus
    #endif
    #endif
    Now create a C file say myCfile.c as follows
    #include <jni.h>
    #include "Mynative.h"
    #include <stdio.h>
    #include "windows.h"
    #include "myold.h" //Header file of the existing dll..you need this file!!
    JNIEXPORT jint JNICALL
    Java_Mynative_getVersion(JNIEnv *env, jobject obj)
    jint myInt;
    /* Call native function in Mynative.dll */
    myInt = getVersion();
    printf(myInt);
    printf("\n");
    return myInt;
    Now compile this file using a C compiler to create "mynative.dll".You'll have to know how to do that using whatever C/C++ compiler you use.
    This dll should be in any folder thats on the Java Classpath or else you are going to get an error.
    Well..you're set. You can run the java file and see the output.
    If however you do not have the header file for myold.dll, then you'll have to know how to dynamically load a dll in C/C++. Unfortunately, i don't know how so u'll have to ask someone else.
    Hope my efforts here don't go wasted. Good luck
    Vinny.

  • JNI using tools or by "hand"?

    I am going to be honest and upfront here, I am posing this question because I am scheduled to give a presentation to a lot of people including my Boss and his Boss and even her Boss in the begining of October. I have tried to gather some information on the web about JNI tools and have found that thare are more than a few ways to do JNI but no one really says how much they liked/disliked each method. Since JNI is just a part of my presentation (I figure about 45 minutes worth) I don't have time to try each one of the many options out. In return for any information, if i get enough information to make it worthwhile, I will post the compiled list including credit for information provided to the java community websites.
    What I need to know is what you have tried, how it worked for you, and pro's you found, any con's, and if you would recomend it to another.
    For example I have tried swig a little.
    Tool: Swig
    How it worked for me: It got the job done after a lot of manipulation of the C code.
    Pros: It was fast and relativly easy on the java side.
    Cons: I had to write tons of wrappers to the C code because there was tons of crap swig could not take, arrays were a big hastle with swig. The java generated code was also really hard to read and seems horribly inefficient.
    Would I recomend it: NO!
    More background info if anyone is interested: I was tasked with this because I suggested we stopped using so many questionable tools to autogenerate code for us and JNI was one of the areas I pointed out we could benefit by doing it by hand. Though I have never done it by hand myself. Since a huge portion of our work is maintaining the code, if we learn how to write JNI by hand and do so not only will the JNI be more readable and efficient, but we will actually be able to maintain it without adding wrappers to C further slowing things down and increasing overhead. Am I wrong?

    JustSomeGuy wrote:
    Have you tried swig before? We could not modify it to get it to play nice with some types, especially any array that is not a standard single dimension array. We ended up having to use C wrappers.That by itself would not be a reason to dismiss all code generation.
    jschell wrote:
    Nothing you said here would lead to that conclusion for me.
    In terms of "messy", because it seemed awkward or because it required a lot of manual steps?
    If the second then why not simply automate that process.
    If the first that isn't a reason. Generated code doesn't need to look nice. Basically, we write a GUI that runs about 50 different functions in C and a few fortran ones and displays their results as a model. Those functions each have a team of programmers that is constantly changing them, often these changes include changes that affect JNI code, but usually only one small portion of it. Presumably each function actually consists of a large number of methods, rather than for example, just a couple where some new data types are added.
    Presumably a meta data solution would not work either - where a C layer provides a generic structure (name/value pairs are the easiest example) and then changes are just added generically.
    For changes that occur frequently one should probably consider a dynamic interface anyways unless one can demonstrate that such a design is detrimental.
    The generated code makes it impossible to read and edit by hand because it is really obfuscated, so we have been regenerating the code every time, which is not always a quick/easy process. By obfuscated you mean hard to read correct? Rather than as in the java term where class files are reworked to make reverse engineering harder.
    Why is is not easy?
    Being able to simply change easy to read JNI code by hand would be much faster for us in almost every situation. That is of course unless you know of a better tool than swig, which would be why I created this thread.
    The explanation is not clear. A code generation tool is a run and forget type of operation. It might take a while to actually run but that is a different problem. If that is the problem is it because the make process is monolithic in that everything is regenerated?
    So, since you seem to be knowledgeable, what tools have you tried and how did they work out for you?
    I have been creating my own code generators for more than 10 years. Mostly build time but sometimes run time as well.
    Noting of course that creating your own tool could be a possibility.

  • Oracle Java Store Procedure and JNI

    Hi ! 'Is the anybody out there ?'
    We don't know. We are developing a java store procedure in Oracle 9i. We need to use a propetary .so file about cript/decript information (entrust file). So have to use in oracle a .jar file with all software infrastructure that use this .so file. But, we have a problem. 'Cause, in windows, we can use a .dll file from our java store procedure, but when we run under unix aix, we have problem about jni.
    java.library.path in oracle console is empty.
    So we cannot find our share object... have you got an idea ?
    thanks!!
    andrea

    Oracle lite does not support the use of PL/SQL procedures or triggers in the database, so unfortunately you just cannot use them.
    as an alternative you need to either
    a) include the setting on the client records when they are created
    b) use before insert/update triggers on the main oracle database to populate the columns. NOTE if doing this you will find that the user that is actually responsible for the inserts and updates to the main database is MOBILEADMIN (as it is running the apply process. If you just want to log the fact that it was client created fine, but will not tell you the creating user)
    c) you should be able to use pure java stored procedures on the client (if in the main oracle database, they should replicate across, but beware of jvm differences and any advanced stuff that may not be supported. you will also have to get any necessary jar files over to the client as well)
    We have used method a) as a standard method called for all inserts/updates in the java client APPLICATION software based on the existance of the audit columns, rather than within the database as it is simpler

  • Compiling javah in eclipse(jni)

    hi all,
    i m writing a java code to access c++ dll files.
    I have written the java class and save it with hello.java.
    i dont know how to compile it with the javah and -jni.
    I am using eclipse. i have also downloaded cdt tool.
    also if there is an easy way to generate jni file for the c++, that would save my time.
    as i want to save my time.
    providing solution would be so nice of you.
    best regards

    It sounds like you may have misconceptions about JNI. Have you done any JNI tutorials? Start simple - until you know how something works, tools aren't going to be very easy to use.
    This one may help:
    http://codequirks.blogspot.com/2008/06/introduction-to-jni.html

Maybe you are looking for