Using native interfaces in java

How can you use a native interface in java?

1) By reading the JNI api and writing appropriate code or
2) Stealing some code already done to get you started
(#2 is VERY popular)
And, oddly enough, I have posted 2 articles on here about using JNI in the last few weeks. So, do a search for "smg123 JNI" and you'll find my posts.

Similar Messages

  • Using component interface in Java

    i know working and programming with component interface but no knowledge about java
    please give me steps to use component interface in java.
    Shiv

    1. Identify/Create Component Interface which you want to access in Java
    2. Apply security for your Component Interface
    3. Open the Component Interface in Application Designer
    4. In Application Designer, select Build > PeopleSoft APIs
    5. A dialog box comes with caption "Build PeopleSoft API Bindings"
    6. Select Build checkbox for Java Classes Frame and give a target directory
    7. Select OK, This will generate PeopleSoft API classes, dont panic if it takes some time
    8. Right click on component interface, select Generate Java Template, this step will generate Java Template
    9. If you are not familiar with compiling and setting up CLASSPATH in Java then it is better you ask any of your colleagues who knows Java.
    10. Once CLASSPATH is set properly you can compile the generate Java Template without any problems
    11.after compilation you will get a .class file and you need to execute that .class file
    12. When you execute the .class file it will ask application server name, application server port, application user name and application password.
    If you give all the information correctly then you will be accessing CI in Java.
    Let me know if you need more help on this.
    Jayaprakash Tedla

  • How to Use native keyword in java programming

    Hi ,
    I am using JDK 1.6.0_11 , and i was trying to create a java program using "native" keyword ,
    i got the sample code for the same from the site : - http://www.javaworld.com/javaworld/javatips/jw-javatip23.html
    But when i type this command " C:\javah -stubs Happy " following error occurs
    " Error: JNI does not require stubs, please refer to the JNI documentation. "
    then typed " c:\javah -jni Happy" without any error .
    After that i wrote a HappyImpl.c as mentioned in the above tutorial
    #include <StubPreamble.h> /* Standard native method stuff. */
    #include "Happy.h" /* Generated earlier. */
    #include &ltstdio.h> /* Standard C IO stuff. */
    void Happy_printText (struct HHappy *this)
    puts ("Happy New Year!!!");
    then it is not compling and error is
    "unable to open included file StubPreamble.h file
    unable to open included file Happy.h file "
    Please help me . i want to use native method , i did exactly the same in tutorial at above link.
    Thanks & Regards
    Mannat

    you do need to know how to use your C compiler... It quite clearly can't find those files which indicates that you didn't tell it where to find them.

  • How to use DLL files in java?

    hi!
    i am working on hardware based java programming. the drivers which i have are in dll format. the manufacturers dont have a java based driver api's for the hardware. i want to invoke the dll file using a bridge between windows invironment and jvm invironment. more over i am not preferring to use native interface interface and c program to tackle this problem.
    is there a way out?

    Is it a COM dll? You could use JACOB. If it's not COM, there's also JNA which as I understand it, is simpler than JNI, although I've never used it
    Edited by: georgemc on Aug 22, 2008 11:05 AM links not working at moment for some reason
    Edited by: georgemc on Aug 22, 2008 11:10 AM

  • Change volume and Balance of speaker using Clip interface

    Hello,
    I am playing more than one Clip simulteniously. I want to change the volume and balance of individual Clip so that they have individual effect on speaker. I mean, if there are two clip then one should have high sound and other have low like that.
    Any help would be appreciated.
    Thanks in Advance.

    Hello,
    I play a sound using Clip interface in Java Sound. i am able to get FloatControl.Type.MASTER_GAIN) control but not FloatControl.Type.PAN)
    I have written a code like this,
    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.PAN);
    Any help would be appreciated.
    * Thanks In advance*

  • How to use Native SQL statement in JDBC receiver interface

    Dear All,
    Can any one please help us in using Native SQL statement in a JDBC receiver channel. The reason why I need to use Native SQL statement instead of standard XML structure is that I need to execute a dynamic SQL query in third party database system lke:-
    Select Field1 Field2 from TABLE Where Field3 like "%Name'
    I expect the the response in the form of XML file which I can pick up using synchornous interface as mentioned on help.sap.com:-
    http://help.sap.com/saphelp_nw04/helpdata/en/64/ce4e886334ec4ea7c2712e11cc567c/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/64/ce4e886334ec4ea7c2712e11cc567c/frameset.htm
    The value for %Name can change dynamically according to the transaction and hence cannot be inluded as a KEY element in standard XLM structure,
    Hence I need to know:-
    1. What message mapping I should use in case if I have to use Native SQL statement.
    2.What operation mapping I should use in case if I have to use Native SQL statement.
    If guess correclty I may have to use Java mapping to do the above activities. Hence I want to know
    3 .How do to go about it to do the Java mapping.
    Thanks
    Ameet

    >
    Ameet Deshpande wrote:
    > Dear All,
    >
    > Can any one please help us in using Native SQL statement in a JDBC receiver channel. The reason why I need to use Native SQL statement instead of standard XML structure is that I need to execute a dynamic SQL query in third party database system lke:-
    >
    > "
    > Select Field1 Field2 from TABLE Where Field3 like "%Name'
    > "
    > I expect the the response in the form of XML file which I can pick up using synchornous interface as mentioned on help.sap.com:-
    >
    > http://help.sap.com/saphelp_nw04/helpdata/en/64/ce4e886334ec4ea7c2712e11cc567c/frameset.htm
    > http://help.sap.com/saphelp_nw04/helpdata/en/64/ce4e886334ec4ea7c2712e11cc567c/frameset.htm
    >
    > The value for %Name can change dynamically according to the transaction and hence cannot be inluded as a KEY element in standard XLM structure,
    >
    > Hence I need to know:-
    >
    > 1. What message mapping I should use in case if I have to use Native SQL statement.
    > 2.What operation mapping I should use in case if I have to use Native SQL statement.
    > If guess correclty I may have to use Java mapping to do the above activities. Hence I want to know
    > 3 .How do to go about it to do the Java mapping.
    >
    > Thanks
    > Ameet
    You can use a stored procedure, and call it from jdbc receiver adapter.
    I also solve this issue, with a DBLookup in message mapping. You can refer to my blog, and this usefull 3d:
    http://simonlesflex.wordpress.com/2010/12/07/pi-oracle-dblookup/
    /people/alessandro.guarneri/blog/2006/03/27/sap-xi-lookup-api-the-killer
    /people/siva.maranani/blog/2005/08/23/lookup146s-in-xi-made-simpler

  • HELP : "LINUX BASH COMMANDS" Java Native Interface

    how can i create a program using Java Native Interface that could run or recognize LINUX BASH COMMANDS

    Runtime.getRuntime().exec(<command>); But read this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • How java support multiple inheritance by the use of interface.

    As per my understanding, Interface is just having the signatures of the methods not the implementation.
    So How java support multiple inheritance by the use of interface?
    Answer 1: we can institate interface reference by its implemented
    class.
              ����� interface inf...
              ����� class aa implements inf..
              ����� class bb implements inf....
               Now, inf i = new aa();
               inf i = new bb();
    Answer 2: We can extends as many interface as we want in the
    single
               interface.
               i.e. interface infFirst....
               interface infSecond....
               interface infThird....
               Now ,
               interface ingMulti extends infFrist, infThird...
    By above two answers its not prity clear as per the multiple inheritance in C or C++.
               i.e.
               class first{
               method abc();....}
               class second{
               method bbc()......}
               class multi::first::second{
               we can call to abc();.....as well as bbc();
    -Please give your important suggstion on the same.(Hope I explain it well.)
    -Jeff

    The keyword implement is used only for interfaces not
    for abstract class. If i am wrong correct me.I believe your right, but I will double check.
    As for the multiple inheritence think about the following code:
    class Animal {
        //  Animal generic stuff in this class
    interface Eat {
        //  Generic stuff that models eating behavior
    interface Runs {
        //  generic methods that model running behavior
    public class Horse extends Animal implements Eat, Runs {
        //  Stuff specific to a horse
    }The Animal class is generic but has stuff in it common to all animals.
    The Eat interface models behavior that is generic to eating, all living things have to eat something to survive. Herbavore are different from carnivores.
    The Runs interface models generic behavior to running, such as speed. A cheeta definately runs faster than a human.
    This brings us to the Horse class. It extends the Animal class because it "is-a" animal, and it implements the eat and runs interface because they are behaviors a horse has.
    I hope that helps.
    Extending an abstract class is the same as extending a regular class with the exception you MUST override all abstract methods in the abstract class. Thats not too difficult but I believe when designing classes, designing an abstract can be more diffecult than modeling the base class, and generic behaviors in interfaces. JMO.
    JJ

  • FATAL ERROR in native method: Wrong method ID used to invoke a Java method

    When calling the same method second time , I get message ::
    <FATAL ERROR in native method: Wrong method ID used to invoke a Java method>
    void myFunction(int myVal)
    JNIEnv *env = NULL;
    jclass odbcconnls;
    jint res;
    printf("\nInitilaizing class ");
    res = (jvm)->AttachCurrentThread((void **)&env,NULL);
    if (res < 0) {
    fprintf(stderr, "Can't get Env \n");
    (jvm)->DestroyJavaVM();
    return SQL_ERROR;          
    if(res == JNI_OK)
    printf("\nThe env is initialized ");
    if(*(&env) == NULL)
    printf(" the env is NULL ");
    printf("\nenv :::::: %s ", env);     
    // the jobject (dbc->actualConn) is a global reference
    odbcconnls = (env)->GetObjectClass(dbc->actualConn);
    if (odbcconnls == NULL) {
    goto destroy;
    switch(myVal){
    case 1:
    jmethodID methodId ;
    jboolean jbool;
    SQLINTEGER Val = (SQLINTEGER )Value;
    SQLINTEGER val1 = *Val;
    methodId = (env)->GetMethodID( odbcconnls,"myFun1","(Z)V");
    if(methodId == NULL){
    goto destroy;
    if(val1 == SQL_FALSE )
    jbool = 0;
    else
    jbool =1;
    env->CallVoidMethod(dbc->actualConn,methodId,jbool);
    env->DeleteLocalRef((jobject)res);
    env->DeleteLocalRef((jobject)odbcconnls);
    env->DeleteLocalRef((jobject)methodId);
    jvm->DetachCurrentThread();
    return ;
    case 2 :
    jmethodID methodId1 ;
    SQLUINTEGER* Level;
    methodId1 = (env)->GetMethodID( odbcconnls,"myFun2","(I)V");
    if(methodId1 == NULL){
    goto destroy;
    Level = (SQLUINTEGER *)Value;
    env->CallVoidMethod(dbc->actualConn,methodId1,(int)*Level);
    dbc->txn_isolation = (SQLUINTEGER)Value;
    env->DeleteLocalRef((jobject)res);
    env->DeleteLocalRef((jobject)odbcconnls);
    env->DeleteLocalRef((jobject)methodId1);
    jvm->DetachCurrentThread();
    return ;
    case 3 :
    SQLCHAR* Cate;
    jmethodID methodId2 ;
    jstring jStrCat;
    methodId2 = (env)->GetMethodID(odbcconnls,"myFun3","(Ljava/lang/String;)V");
    if(methodId2 == NULL){
    goto destroy;
    Cate = new SQLCHAR[20];
    strcpy((char *)CCatalog,(char *)Value);
    jStrCat = env->NewStringUTF((char *) Cate);
    printf("\n got jSTring ");
    env->CallVoidMethod(dbc->actualConn,methodId2,jStrCat);
    printf("\n after called method ");
    int len = strlen((char *)Cate);
    dbc->Cate = new SQLCHAR[len+1];
    strcpy((char *)dbc->Cate,(char *)Cate);
    printf("\n copied result ");
    env->DeleteLocalRef((jobject)res);
    env->DeleteLocalRef((jobject)odbcconnls);
    env->DeleteLocalRef((jobject)methodId2);
    jvm->DetachCurrentThread();
    return ;
    destroy:
    if ((env)->ExceptionOccurred()) {
    (env)->ExceptionDescribe();
    jvm->DetachCurrentThread();
    (jvm)->DestroyJavaVM();
    return SQL_ERROR;
    When case 1 is called second time this error is thrown..
    plz help me..
    Thanx
    MittalSunita.

    When calling the same method second time , I get
    message ::
    <FATAL ERROR in native method: Wrong method ID used
    d to invoke a Java method>
    void myFunction(int myVal)
    JNIEnv *env = NULL;
    jclass odbcconnls;
    jint res;
    printf("\nInitilaizing class ");
    res = (jvm)->AttachCurrentThread((void **)&env,NULL);
    if (res < 0) {
    fprintf(stderr, "Can't get Env \n");
    (jvm)->DestroyJavaVM();
    return SQL_ERROR;          
    if(res == JNI_OK)
    printf("\nThe env is initialized ");
    if(*(&env) == NULL)
    printf(" the env is NULL ");
    printf("\nenv :::::: %s ", env);     
    // the jobject (dbc->actualConn) is a global
    reference
    odbcconnls = (env)->GetObjectClass(dbc->actualConn);
    if (odbcconnls == NULL) {
    goto destroy;
    switch(myVal){
    case 1:
    jmethodID methodId ;
    jboolean jbool;
    SQLINTEGER Val = (SQLINTEGER )Value;
    SQLINTEGER val1 = *Val;
    methodId = (env)->GetMethodID(
    ( odbcconnls,"myFun1","(Z)V");
    if(methodId == NULL){
    goto destroy;
    if(val1 == SQL_FALSE )
    jbool = 0;
    else
    jbool =1;
    env->CallVoidMethod(dbc->actualConn,methodId,jbool);
    env->DeleteLocalRef((jobject)res);
    env->DeleteLocalRef((jobject)odbcconnls);
    env->DeleteLocalRef((jobject)methodId);
    jvm->DetachCurrentThread();
    return ;Why do you delete a local reference???
    Did you ever call the get local reference?
    case 2 :
    jmethodID methodId1 ;
    SQLUINTEGER* Level;
    methodId1 = (env)->GetMethodID(
    ( odbcconnls,"myFun2","(I)V");
    if(methodId1 == NULL){
    goto destroy;
    Level = (SQLUINTEGER *)Value;
    env->CallVoidMethod(dbc->actualConn,methodId1,(int)*Le
    el);
    dbc->txn_isolation = (SQLUINTEGER)Value;
    env->DeleteLocalRef((jobject)res);
    env->DeleteLocalRef((jobject)odbcconnls);
    env->DeleteLocalRef((jobject)methodId1);
    jvm->DetachCurrentThread();
    return ;
    case 3 :
    SQLCHAR* Cate;
    jmethodID methodId2 ;
    jstring jStrCat;
    methodId2 =
    (env)->GetMethodID(odbcconnls,"myFun3","(Ljava/lang/St
    ing;)V");
    if(methodId2 == NULL){
    goto destroy;
    Cate = new SQLCHAR[20];
    strcpy((char *)CCatalog,(char *)Value);
    jStrCat = env->NewStringUTF((char *) Cate);
    printf("\n got jSTring ");
    env->CallVoidMethod(dbc->actualConn,methodId2,jStrCat)
    printf("\n after called method ");
    int len = strlen((char *)Cate);
    dbc->Cate = new SQLCHAR[len+1];
    strcpy((char *)dbc->Cate,(char *)Cate);
    printf("\n copied result ");
    env->DeleteLocalRef((jobject)res);
    env->DeleteLocalRef((jobject)odbcconnls);
    env->DeleteLocalRef((jobject)methodId2);
    jvm->DetachCurrentThread();
    return ;
    destroy:
    if ((env)->ExceptionOccurred()) {
    (env)->ExceptionDescribe();
    jvm->DetachCurrentThread();
    (jvm)->DestroyJavaVM();
    return SQL_ERROR;
    When case 1 is called second time this error is
    thrown..
    plz help me..
    Thanx
    MittalSunita.

  • Java Native Interface Exception Access Violation

    Ok i have two programs One that works and one that dosn't
    the one that works is a javaMethod that calls a C method. the C method then uses a callsback on a java method that returns an array of integers. that works fine
    the Second program uses the same c side code and the same Java Methods from the original are implemented into a program i have that runs a Jframe. anyways when i run this program the Native side does all of its tasks but when its done it crashes Evrything
    here is the Native Code
    #include <jni.h>
    #include "MyNativeInterface.h"
    #include <stdio.h>
    JNIEXPORT void JNICALL
    Java_MyNativeInterface_sayHello(JNIEnv *env, jobject obj)
    printf("Hello world!\n");
    return;
    JNIEXPORT void JNICALL
    Java_MyNativeInterface_nativeMethod(JNIEnv *env, jobject obj)
    jintArray arr;
         jint len;
         int *carr;
         int i;
    jclass cls = (*env)->GetObjectClass(env, obj);
    jmethodID mid =
    (*env)->GetMethodID(env, cls, "callback", "()[I");
    if (mid == NULL) {
    return; /* method not found */
    printf("In C\n");
    arr = (*env)->CallObjectMethod(env, obj, mid);
         len = (*env)->GetArrayLength(env,arr);
         printf("Array Length = %d\n",len);
         carr = (int*)malloc(len);
         (*env)->GetIntArrayRegion(env,arr,0,len,(jint*)carr);
         for (i=0;i<len;i++)
    printf("Array position %d is %d \n",i,carr);
    and here is the java class that deals with the native side
    import java.util.*;
    public class MyNativeInterface extends Object {
    private TextAreaDemo gui ;
    public MyNativeInterface (TextAreaDemo guiRecieved)
    gui = guiRecieved;
    static {
    System.loadLibrary("hello");
    public boolean sendLocoMessage ( byte [] bytesToSend )
    try {
    System.out.println("number Of Tokens ="+bytesToSend [0]);
    } catch (Exception e) {
    System.out.println("didn't even get inside2 ok ");
    return true;
    public void receiveLocoMessage ( byte [] bytesToSend ){
    gui.AddMessage ("WOOOOO HOOOOOO");
    public native static void sayHello ();
    public native void nativeMethod();
    private int [] callback() {
    int cools [] = new int [32] ;
    int i = 0 ;
    for (i=0;i<32;i++)
    cools [i] = i;
    //System.out.println("In Java");
    return cools;
    the Say Hello Method works just fine
    and the array for the C side prints out just fine
    then evrything crashes with the following message
    An unexpected exception has been detected in native code outside the VM.
    Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x6D3276E4
    Function=ZIP_Open+0x107
    Library=c:\j2sdk1.4.0_01\jre\bin\zip.dll
    and a whole BUNCH of current java thread stuff and a list of dynamic libraries.
    anyways thanks

    Your C code asks for the length of the array, then
    allocates that many BYTES using malloc. This looks
    bogus to me.I agree.
    Maybe something like this would be better:
    carr = (int*)malloc(len * sizeof jint);

  • How to Use DLL Function in Java Applet

    Hi all,
    I have been assigned a task to develop java applet. The problem is, I need to use DLL functions in my applet to read records. Could you pls anyone guide me how to interface DLL and applet to read records with sample code?
    I'm using JDK 1.5.0_06 and Windows XP OS. thanx..
    best rgds,
    jpdbay

    You will need to use Java Native Interface JNI. Ther are many posts on the subject, please search, and this is the documentation:
    http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

  • How to use HTML parameters in Java applet

    Hello
    In perl I have created a form with things like a selectiuon box called currency and text fields called product for example. Then when I click submit the perl program can pick up the currency and product parameters.
    I have read about applet param eg <param name="param1" value="Apple"> is this the same thing? Or is it different?
    Can I use HTML parameters or do I need to use applet param? I am confused.
    If applet param is what I need, then how do I change the param values say by editing text in a text field?

    You will need to use Java Native Interface JNI. Ther are many posts on the subject, please search, and this is the documentation:
    http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

  • Using Fortran Funcrions in Java

    Hi,
    In my project I need to call some subroutine, made in fortran, into my java program but I don't know how to do it...
    My fortran subroutine is called: SUBROUTINE LETQUE(BUF,QUE,SIZE,BYPASS)
    What should i do to include in my java???
    Thank,
    BV

    You may be able to access FORTRAN functions from Java using the Java Native Interface (JNI). Here's the JNI tutorial, which only has examples in C: http://java.sun.com/docs/books/tutorial/native1.1/index.html
    You'll probably want the trail "Writing Java Programs with Native Methods". Your biggest challenge will be marshalling and unmarshalling the input parameters and return values of your FORTRAN fucntions--that is, converting the data back and forth between Java and FORTRAN data formats. You are probably going to have to do some experimenting to get each data type to translate between languages, and should be very familiar with the format of your FORTRAN function's parameters, down to the byte level. You will also need to find out how to create a shared library (on Unix) or a DLL (on Windows) from a FORTRAN source file.
    Try searching the forums for "fotran". I see a lot of people are creating intermediate C-language wrappers for their FORTRAN routines, and calling the C from JNI. That might be an easier path.
    Another possibility, if you're processing large amounts of data in batches, would be to use text files to exchange data between a stand-alone Java application and a stand-alone FORTRAN program.
    Good luck!

  • Using C++ Library in Java

    Hi,
    How do I use a prewritten C++ API in Java? With C++ I have to use the include files that came with the DLL. I have no access to the code. I'd appreciate reasonably extensible code examples.
    Thanks.

    You can use JNI(Java Native Interface) to solve this problem.
    I give an example below, you can do advanced functions in depth.
    First, write a simle class named Test, it contains one method called getSquare which pass a int argument only. Of course It contains the program entry.
    public class Test
    public static void main(String args[])
    //import dll and print returned result
    try{
    System.loadLibrary("Test.dll"); //we will name the compiled program as "Test.dll"
    }catch(Exception e){}
    System.out.println(Test.getSquare(5));
    public static native int getSquare(int num);
    As you can see, It is very easy. You need native method to access functions which wrote in C or C++. The native method like abstract do not have method body, it has signature only, we will write method body in other programming language such as C or C++.
    Next step, likes we did normal, compile it(javac Test.java), then we will use JNI compiler tool which in Java bin directory compile the Test class(javah Test), note never specify the suffix ".class" or ".java". After the command executed, you will find Test.h in the same directory. That is, it is the header file of C or C++, it is the code skeleton, next step we will write function body.
    You may complain the function's signature is too long to rember, don't worry, it is needless, just copy it to your C file or C++ file, then imply it. here the fountion returns the square of the argument.
    JNICALL jint methodSignature(JNIEnv *env,jint arg)
    return arg*arg;
    Excuse me, I forget the functionsignature too, but there is a rule with it, so replace it in any signature(when you coding, never change the signature, it was created by compiler automatically).
    At this step(imply the function), you can do as you like include invoke dlls, execute applications even thougn win32 founctions. Note, there are too many data types(jint, jshort,jstring), functions and structs and pointers defined in JNI environment, you must know the change C or C++ type to java types. If you know much about C or C++ and Java, you can read jni.h file which in Java include directory, it is important to read books or articles about JNI.
    compile the code, name it with "Test.dll"
    Execute your java class, what did you find?

  • Using C++ code in java

    is there any ways to use a C++ written file in a java file?

    You are probably looking for JNI = Java Native Interface. Do a web search on that, there are tutorials on this site

Maybe you are looking for