Call Java method from c/c++ from native thread

Hi,
I know how to call a method from c/c++ with jni back to Java
but this fails when I try to do this from a native thread.
I can get the object class (jclass) but its seems to fail to get a method from that class.
How to make it work?
Thanks in advance.

Ok, problem has been solved
I just get the jmethodID inside a JNI method and pass it to the thread.

Similar Messages

  • In JNI how to call Java methods from native language methods

    I just build the example of java tutorial on
    http://java.sun.com/docs/books/tutorial/native1.1/implementing/method.html
    but when I excute, it says:
    Callbacks.c:1:syntax error before 'void'
    Callbacks.c:2:parse error before '*'
    Callbacks.c:In function 'Java_Callbacks_nativeMethod':
    attached:
    Callbacks.c
    JNIEXPORT void JNICALL
    Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
    jclass cls = (*env)->GetObjectClass(env, obj);
    jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "(I)V");
    if (mid == 0) {
    return;
    printf("In C, depth = %d, about to enter Java\n", depth);
    (*env)->CallVoidMethod(env, obj, mid, depth);
    printf("In C, depth = %d, back from Java\n", depth);
    Callbacks.java
    class Callbacks {
    private native void nativeMethod(int depth);
    private void callback(int depth) {
    if (depth < 5) {
    System.out.println("In Java, depth = " + depth + ", about to enter C");
    nativeMethod(depth + 1);
    System.out.println("In Java, depth = " + depth + ", back from C");
    } else
    System.out.println("In Java, depth = " + depth + ", limit exceeded");
    public static void main(String args[]) {
    Callbacks c = new Callbacks();
    c.nativeMethod(0);
    static {
    System.loadLibrary("MyImpOfCallbacks");

    Looks like you havn't included the jni header file. Add this to the top of the .c file:
    #include "jni.h"

  • How to call java method from C ?

    Hello,
    I try to call a native method from java which calls java method from the same class. Exception (noSuchMethodError) is thrown. Any ideas?
    Thanx in advance !
    here is the source of the native method :
    JNIEXPORT jint JNICALL Java_TestDll_Proc_1Mul_1Int_1Var_1Var_1Stdcall (JNIEnv * env, jclass jcl, jint jarg1, jint jarg2) {
    int arg1;
    int arg2;
    int arg3;
    jint res;
    char * ch = "test";
    jfieldID fid;
    jmethodID mid;
    int (* procedure) (int *, int * ,int *);
    int mch;
    arg1 = (int )jarg1;
    arg2 = (int )jarg2;
    procedure = GetProcAddress(libraryHandle,"Proc_Mul_Int_Var_Var_Stdcall");
    procedure(&arg1,&arg2,&arg3);
    res = (jint) arg3;
    printf("(*env)->GetMethodID(env, jcl, \"test\", \"()V\");\n");
    mid = (*env)->GetMethodID(env, jcl, "test", "()V");
    printf("(*env)->CallVoidMethod(env, jcl, mid);\n");
    (*env)->CallVoidMethod(env, jcl, mid);
    return res;
    here is the source of the java file:
    public class TestDll {
    static {
    System.loadLibrary("testdllwrap");
    System.out.println("java: Library testdllwrap.dll loaded");
    public TestDll() {
    public native int Proc_Mul_Int_Var_Var_Stdcall(int jarg1, int jarg2);
    public void test() {
    System.out.println("java: test()");
    public static void main(String[] args) {
    TestDll access = new TestDll();
    int a = 5;
    int b = 6;
    int c = 0;
    System.out.println("Calling Proc_Mul_Int_Var_Var_Stdcall");
    c = access.Proc_Mul_Int_Var_Var_Stdcall(a,b);
    System.out.println("Java Result = " + c);

    Something is wrong with the code you posted here.
    Since your native method is not static, it should have the jobject instance as a parameter in the function prototype, not a jclass. Also, you should be calling CallObjectMethod with a jobject, not jclass.
    Check out Jace at http://jace.reyelts.com/jace.
    To call the java method you would do:
    JNIEXPORT jint JNICALL Java_TestDll_Proc_1Mul_1Int_1Var_1Var_1Stdcall
    (JNIEnv * env, jobject jTestDll, jint jarg1, jint jarg2) {
      TestDll testDll( jTestDll );
      testDll.test();
    }God bless,
    -Toby Reyelts

  • How to call Java method from XSLT??

    Hi All,
    Jdev 11.1.1.3.0
    I have a requirement to implement that, I have to call Java method from XSLT. Could anyone please suggest to implement that??
    Thanks,
    Santosh M E

    As pointed by others, you must expose your method as a custom function, registering with JDeveloper (for development time) as well as with SOA Suite (for runtime).
    In the link below you will find a simple step by step example:
    https://blogs.oracle.com/reynolds/entry/building_your_own_path
    Regards,
    Luis F. Heckler

  • How to call java method from workflow script?

    Hi
    I have a requirement of updating field value 'Document Status' based on review/approve of content from Workflow and hence need to update the version number. For that I need to call my java method from workflow during submit of review/approve condition. Please let me know how to call java method from workflow?
    Is there any alternative better way to achive this requirement from workflow? Please suggest.
    Thanks,
    Sarang

    OK. So, I think we can all conclude that you don't need to call any Java method, can't we? And, that wfUpdateMetadata is the command that will update your metadata.
    Now, the question is what are its arguments. It has two - the first is the name of a custom metadata field to be updated (let's suppose that one field is called xMinorVersion, and the other xMajorVersion), the other is the new value, e.g. <$wfUpdateMetaData("xMinorVersion", "New value.")$>As for new value - do you insist on using strings? Since you want to increase the value, it would be more convenient to work with numbers. For instance, with integers you could go with <$wfUpdateMetaData("xMinorVersion", xMinorVersion + 1)$>With strings you will need to convert it to numbers and back to strings. Besides, what happens if you have more than 100 minor versions? (you mentioned you want to add 0.01, but that would finally increase the major version, wouldn't it?) So, I think these two numbers are independent (perhaps, with exception that increase on the major version set the minor version to .00).
    If you want to present it, you can use profiles that will construct for you the representation 2.304 out of MajorVersion = 2, MinorVersion = 304
    Solved?

  • Call Java Method From JavaScript Function

    hi everyone
    i need a help in calling Java method from a javaScript method
    ex:
    function confirmAddRecord() {
    cHours =document.getElementById('frmP:ChargeHours').value;
    cSTime =document.getElementById('frmP:ChargeStartTime').value;
    var answer = confirm("Are you sure you want to add Record?")
    if (answer){
    here i want to call the Java Method that is located in session bean that takes the upper params cHours & cSTime
    else{
    return false;
    i know i can do it as an action button but it is required me to be in that way can any one help plz
    Message was edited by:
    casper77

    That depends on the nature of your parameters. I guess you calculate the params on client and then want to submit them. In this case and if you don't want to use Ajax simple add some <input type="hidden"> elements (of course there correspondend components dependent of your framework) and store the params there. If the javascript isn't invoked by a button click, you can use a button nevertheless. Set visible="false" and call
    document.getElementById('client_id_of_my_hidden_button').click();
    (or maybe doClick() dependent on your framework).

  • Calling Java methods from C program

    After a year or so using these excellent forums to answer my queries, without ever having to post a question, I've finally had to ask for help with this one. I've already looked through the JNI tutorial, and these forums, extensively, but I'm still not quite sure of how best to solve my problem.
    I need to call Java methods from a C program. I've already used JNI to call a simple C program from Java, and that's fine, but I just can't determine exactly what I need to do for the reverse to work.
    What is the definitive solution for calling Java from C? Is it to use the invocation APIs to create a JVM, and then access it through the various GetMethod, FindClass methods? Or can I 'reverse engineer' the process I followed to get Java to call C?? The tutorial is a bit confusing, as the example it gives uses Callback stuff, but I guess that's only useful when you want to go Java-C-Java??
    Advice appreciated,
    Rich

    Forgive my blatant bumping, but anyone, please?

  • Calling Java Methods from C++

    Hi ppl,
    I'm a real java newbie but I'm working on a task which requires me to call java methods from a C++ program. Does anyone have any code snippet for me to start of on, say, a c++ prgram which calls a java method that prints "Hello World :oD " to the screen? Many thanks.
    Tyler

    1. Have you looked at the tutorial?
    (Look elsewhere on this website.)
    2. Have you considered buying a book?
    (Try "essential JNI", by Rob Gordon.)

  • Calling Java Methods from Stored Procedures

    Can I call Java Methods from Oracle Stored Procedures? I have a Java framework that logs events and would like to reuse it for logging events that occur in stored procedures.
    null

    You need to publish java class methods to plsql.
    Attached below is some information.
    Although both PL/SQL modules and Java classes are stored in the database
    and are managed by many of the same mechanisms, each of them resides in
    its own namespace. Therefore, Java methods are not accessible from SQL
    and PL/SQL by default. In order to expose Java methods to the SQL and
    PL/SQL engines, first publish that Java method to the SQL namespace using
    a 'Call Spec'.
    Note: A 'Call Spec' does not create an additional layer of
    execution so there is no performance penalty incurred.
    A 'Call Spec' is simply a syntactical mechanism used to
    make a method known in the SQL namespace.
    The SQL name established by the 'Call Spec' can be top-level or packaged.
    The syntax differs only slightly and is consistent with that used for
    PL/SQL procedures and packages. For more information on the exact
    syntax, see the references listed in 'Related Topics'.
    In general, a top-level procedure 'Call Spec' takes the form:
    CREATE OR REPLACE PROCEDURE procname ( pname mode ptype, ... )
    AS LANGUAGE JAVA NAME 'javaname ( javatype, ... )';
    Where: procname is the SQL name you wish to publish
    pname is the name for a parameter to procname
    mode is the parameter mode (i.e. IN, OUT, IN OUT)
    ptype is a valid SQL type (e.g. NUMBER, CHAR, etc.)
    javaname is the fully qualified name of the Java method
    javatype is a Java type for the corresponding parameter
    Likewise, a top-level function 'Call Spec' takes the form:
    CREATE OR REPLACE FUNCTION fname ( pname mode ptype, ... ) RETURN rtype
    AS LANGUAGE JAVA NAME 'javaname ( javatype, ... ) return javatype';
    Where: fname is the SQL name you wish to publish
    rtype is the SQL return type of the function
    Note: Within the NAME clause, everything within quotes is case
    sensitive. For example, if the keyword 'return' is in all
    CAPS, this Call Spec will not compile.
    Other optional parts of this syntax have been omitted here for simplicity.
    Additional examples in subsequent sections illustrate some of these options.
    eg
    CREATE PROCEDURE MyProc (rowcnt IN NUMBER, numrows OUT NUMBER)
    AS LANGUAGE JAVA NAME 'MyClass.MyMethod(int, int[])';
    There are several important things to note here:
    1.) The 'Call Spec' for a JSP must be created in the same schema as the
    corresponding Java class that implements that method.
    2.) IN parameters are passed by value. This is the only parameter mode
    available in Java. OUT parameters, therefore, must be passed as single
    element arrays in order to emulate pass by reference.
    3.) Parameter names do not need to match, but the number and types of
    the parameters must match (with just one exception - see item 5 below).
    Oracle 8i supports conversions between an assortment of SQL and Java.
    See the references listed in 'Related Topics' for additional information.
    4.) Primitive types (e.g. int, float, etc.) are not required to be fully
    qualified with any package name. However, standard Java object types
    (e.g. String, Integer, etc.) as well as any user defined object types
    (e.g. like those generated by JPublisher) must be prefixed with a
    corresponding package name (e.g. java.lang) if applicable.
    5.) The 'main' method which takes a single String[] parameter can be
    mapped to any PL/SQL procedure or function which takes some number
    of VARCHAR2 or CHAR type IN parameters. For example, the java method:
    public static void main ( String[] args ) { ... }
    can be mapped to each of the following:
    PROCEDURE MyProc2 ( arg1 IN CHAR ) ...
    PROCEDURE MyProc3 ( arg1 IN CHAR, arg2 IN VARCHAR2 ) ...
    PROCEDURE MyProc4 ( arg1 IN VARCHAR2, arg2 IN VARCHAR2 ) ...
    and so forth. Parameters map to the corresponding element of the String
    array (e.g. arg1 -> args[0], arg2 -> args[1], etc.).
    null

  • How to call Java methods from a Windows application?

    Hello all,
    At our company, we need to integrate our product which is a Java Swing application with a Windows application. Specifically, we are trying to call Java methods that reside in our application from the Lotus Notes email client application, which is a native Windows application. Such that when a user clicks a button in the Lotus Notes email client, it will trigger an event in our Swing application. Is this possible using JNI? Do you know of any resources or references relating to this kind of a project?
    Thanks,
    Mete Kural

    If there is some dll interface that lets Lotus Notes load up and use a DLL, then yes, this should be possible.

  • Problem calling java method from c

    Hi ,
    I'm trying to call a java method from a C program. it gives no error during compilation as well as building the application. but when i tried to create the JVM by running my application it pops up the message "The application failed to start because jvm.dll was not found. Re-installing the application may fix the problem." I tried out setting all the environment variables to include the jvm.dll(PATH set to c:\j2sdk1.4.2_05\bin;c:\j2sdk1.4.2_05\jre\bin). Still got the same message. Then i re-installed java platform once more. Even now i get the same error. I have more than one jvm.dll at locations jre\bin\client and server, oracle has some jvm.dll . Will that be a problem? if so can i remove those? which of them should be removed and how?
    The code i'm using is
    #include <stdio.h>
    #include <jni.h>
    #include <windows.h>
    //#pragma comment (lib,"C:\\j2sdk1.4.2_05\\lib\\jvm.lib")
    JavaVM jvm; / Pointer to a Java VM */
    JNIEnv env; / Pointer to native method interface */
    JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization requirements */
    int verbose = 1; /* Debugging flag */
    FARPROC JNU_FindCreateJavaVM(char *vmlibpath)
    HINSTANCE hVM = LoadLibrary("jre\\bin\\server\\jvm.dll");
    if (hVM == NULL)
    return NULL;
    return GetProcAddress(hVM, "JNI_CreateJavaVM");
    void main(int argc, char **argv )
    JavaVM jvm = (JavaVM )0;
    JNIEnv env = (JNIEnv )0;
    JavaVMInitArgs vm_args;
    jclass cls;
    jmethodID mid;
    jint res;
    FARPROC pfnCreateVM;
    JavaVMOption options[4];
    // jint (__stdcall pfnCreateVM)(JavaVM *pvm, void **penv, void *args) = NULL;
    options[0].optionString = "-Djava.compiler=NONE"; /* disable JIT */
    options[1].optionString = "-Djava.class.path=c:/j2sdk1.4.2_05/jre/lib/rt.jar"; /* user classes */
    options[2].optionString = "-Djava.library.path=lib"; /* set native library path */
    options[3].optionString = "-verbose:jni"; /* print JNI-related messages */
    /* Setup the environment */
    vm_args.version = JNI_VERSION_1_4;
    vm_args.options = options;
    vm_args.nOptions = 4;
    vm_args.ignoreUnrecognized = 1;
    JNI_GetDefaultJavaVMInitArgs ( &vm_args );
    pfnCreateVM = JNU_FindCreateJavaVM("jre\\bin\\server\\jvm.dll");
    res = (*pfnCreateVM)(&jvm,(void **) &env, &vm_args );
    // res = JNI_CreateJavaVM(&jvm,(void **) &env, &vm_args );
    /* Find the class we want to load */
    cls = (*env)->FindClass( env, "InstantiatedFromC" );
    if ( verbose )
    printf ( "Class: %x" , cls );
    /*jvm->DestroyJavaVM( );*/
    Could anyone help me solve this problem as early as possible, bcoz i'm in an urge to complete the project.
    Thanks in advance.
    Usha.

    You either have to add to the system path of where is your jvm.dll is located or explicitly link to jvm.dll call GetProcAddress to obtain the address of an exported function in the DLL.

  • Call Java Method from Callback function

    I am writing a JNI wrapper in c++ for a particular event driven DLL. The DLL makes a network connection to another device and then calls a callback function when events are raised on the device. The DLL has 3 basic functions: connect, disconnect, and registerEventListener. RegisterEventListener takes a function pointer which is called each time an event is raised on the device.
    My wrapper DLL exposes connect and disconnect functions via JNI. I can call these functions just fine from my Java code. Now the question... How do I call a Java method from my c++ callback function? I can call a Java method using env->CallXXXMethod(...) from within a function that is accessible to Java but I don't have access to the JNI parameters in my Callback function.
    So how do I call a Java method from a callback function? If this cannot be done then what is the "right way" to handle native event notification with JNI?

    jschell wrote:
    JNI parameters? Meaning what exactly?
    General outline of a callback
    1. Entry
    2. Get the VM, env - there are methods for this
    3. Attach the thread
    4. Get a java object - how depends on what you are doing, but create it or a static reference.
    5. Get the java method
    6. Call the java method.That is exactly correct. The callback function is called from a separate thread so using a cached pointer to JNIEnv, obtained from the original native method, crashes the JVM. The jmethodID and jclass objects (which are needed to call the static Java method) can be cached without problem. The following is the code I used to attach the current thread and call my static method.
    void MyClass::onEvent(int system_id, char* data)
         //get a pointer to the Java Environment
         JNIEnv *env;
         jvm->AttachCurrentThread((void **)&env, NULL);
         //Call the Java method with the newly aquired data
         jstring js = env->NewStringUTF(data);
         env->CallStaticVoidMethod(cls, mid, system_id, js);
    }My last question is about cleanup in this function. When I use NewStringUTF to "convert" my char* to jstring do I need to do anything special to clean up or will the Java garbage collector take care of it since the jstring is being passed to a Java method?
    Thanks for you help

  • Call Java Method from BPM Process

    Hi,
      I have defined a java class with set of methods , and I want to know if there is any way to call those methods from a BPM process..
    Thanks in Advance,,
    Best Regards,,
    Ola Essa..

    Use the spring component context in soa suite instead of ejb. Look for documentation in soa suite developer guide. Only if you have existing ejbs then use ejb adapters.
    See this http://www.oracle.com/technetwork/middleware/soasuite/learnmore/fivecoolusecasesforspring-1885529.pdf
    Vikram
    Message was edited by: VikramFusionApplied

  • How to call java method from actionscript

    I've just now started working on Flex. May be its basic question but I’m not aware of it – how can I call a java method from actionscript. I want to call some java method on double click of a event. Can you please let me know how to proceed on this?

    OK. So, I think we can all conclude that you don't need to call any Java method, can't we? And, that wfUpdateMetadata is the command that will update your metadata.
    Now, the question is what are its arguments. It has two - the first is the name of a custom metadata field to be updated (let's suppose that one field is called xMinorVersion, and the other xMajorVersion), the other is the new value, e.g. <$wfUpdateMetaData("xMinorVersion", "New value.")$>As for new value - do you insist on using strings? Since you want to increase the value, it would be more convenient to work with numbers. For instance, with integers you could go with <$wfUpdateMetaData("xMinorVersion", xMinorVersion + 1)$>With strings you will need to convert it to numbers and back to strings. Besides, what happens if you have more than 100 minor versions? (you mentioned you want to add 0.01, but that would finally increase the major version, wouldn't it?) So, I think these two numbers are independent (perhaps, with exception that increase on the major version set the minor version to .00).
    If you want to present it, you can use profiles that will construct for you the representation 2.304 out of MajorVersion = 2, MinorVersion = 304
    Solved?

  • How to call java method from xsl

    hi friends,
    How to call a java method from xsl, i have a xsl file which will call the java method and retrieve the value and display it to the user. but its work well when i set xalan.jar and xerces.jar and the java class files in my classpath and run as
    java org.apache.xalan.xslt.Process -in navigate.xml -xsl nav-exst.xsl -HTML -out navoutpage.html[b]
    in the command prompt but when i deploy it as web application it gives error as
    [b]Namespace 'MyPack' does not contain any functions[b]

    OK. So, I think we can all conclude that you don't need to call any Java method, can't we? And, that wfUpdateMetadata is the command that will update your metadata.
    Now, the question is what are its arguments. It has two - the first is the name of a custom metadata field to be updated (let's suppose that one field is called xMinorVersion, and the other xMajorVersion), the other is the new value, e.g. <$wfUpdateMetaData("xMinorVersion", "New value.")$>As for new value - do you insist on using strings? Since you want to increase the value, it would be more convenient to work with numbers. For instance, with integers you could go with <$wfUpdateMetaData("xMinorVersion", xMinorVersion + 1)$>With strings you will need to convert it to numbers and back to strings. Besides, what happens if you have more than 100 minor versions? (you mentioned you want to add 0.01, but that would finally increase the major version, wouldn't it?) So, I think these two numbers are independent (perhaps, with exception that increase on the major version set the minor version to .00).
    If you want to present it, you can use profiles that will construct for you the representation 2.304 out of MajorVersion = 2, MinorVersion = 304
    Solved?

Maybe you are looking for

  • How can i remove my primary apple id

    I have 2 email address in my apple ID my apple ID & primary email address is:  ***@yahoo.com & my alternate email addresses is:  ***@me.com my alternate email was created using the primary email in iCloud Setting. but now, I want remove this yahoo em

  • Setting defaults for reminders in iCal

    Is it possible to set a default for reminder in iCal? I would like to set it at e-mail the day before. However, iCal seems to have a reminder default as an alert (which is simply a pop-up), not  e-mail. For every event I creat in iCal, I am having to

  • I'm trying to sync my pc & laptop. It didn't work today. Didn't see buttons on sync options.

    When I bought my laptop last year I set up sync for the computers. When I tried to sync today it didn't work. It has been awhile since I've run sync. I went around in circles with the steps on the help pages. I tried to follow the steps on the sync h

  • File upload, download using ADF UIX and not JSP

    I have examples for the file upload, download using JSP But I want to use ADF UIX. Look and feel in this is impressing and I want to use this. Any one have example for this. Users will select a file from their desktop This will be stored into the dat

  • Unable to install Adobe CS6, have tried everything

    I have been trying to install Adobe CS6 Master on a fresh install of Windows 7 Enterprise 64bit with updates running on a Dell OptiPlex 7010. I think I have gone through every scenario possible and cannot get it to install. Keep getting the: "Install