Calling existing C/C++ dll from Java Prog

Hi,
From within the Java code I want to use the
functionality of an existing C API. The dll's of C are available. Is it possible to directly call the C Api using Jni or some other technique or do I need to implement a native method in C which would Use the existing C Api to get the values and Pass them to the Java prog.
Istikhar.

Is it possible to directly call the C Api using Jni...Depends on what you mean. You can't call it directly from java. You can use JNI, including C/C++ to call it.

Similar Messages

  • Calling a third Party dll from java using JNI

    Hi
    I want an immediate help from u all.
    I have athird party c dll with .h and .lib file
    I wont be able to cahnge any thing in those files.
    I want to call the functions of that from my Java code.
    Ist possible to call the dll without writing any c or c++ wrapper over it?if yes , how to do it?

    Hi,
    You may use a generic wrapper like JNative.
    Commercial wappers also exists.
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                               

  • Fall into a trouble when calling a dll from java in linux

    Hi, experts:
    I encountered a big trouble when using JNI to call a C++-compiled DLL from java in Linux: The DLL function didn't execute immediately after the invocation, and was deferred till the end of the Java execution. But all worked well after migrating to windows. This problem made me nearly crazy. Can somebody help me? Thanks in advance.
    Linux: fedora core 8, jdk1.6.0_10,
    compile options: g++ -fPIC -Wall -c
    g++ -shared

    It looks like the OP compiled the C source file and linked it into a Windows .dll, which would explain why it worked flawlessly in Windows. (Though I must say the usage of GCC would make someone think it was compiled in Linux for Linux, GCC also exists for Windows, so I'm not sure it's a Windows .dll either...)
    @OP: Hello and welcome to the Sun Java forums! This situation requires you to make both a .dll and a .so, for Windows and Linux. You can then refer to the correct library for the operating system using System.loadLibrary("myLibrary"); without any extension.
    s
    Edited by: Looce on Nov 21, 2008 11:33 AM

  • Calling back c++ method in an exe (not a dll) from java?

    Hi all,
    I have to make an hybrid C++/java from an existing C++
    application that compiled is a big single exe not a dll.
    I'm running under win32.
    The application consists of several windows. The hybrid
    will have widows in C++ and some in java. They have the
    same menu and tool bar. In the C++, there are some
    callbacks called when a button is pressed. How to call
    these callback from java given the fact that the JVM and
    the java classes are launched by the exe file.
    I know how, from C++, start JVM and call my java window.
    I also know how to call a C++ method that is in a dll from
    java. It's from the tutorial
    http://java.sun.com/docs/books/tutorial/native1.1/index.html
    But I don't know how to call a C++ method that is in in an
    exe which has launch a JVM from an object running in this
    JVM.
    Is there somewhere an example like this?
    Thanks,
    Xavier.

    Thanks this helped.
    For those who want a complete example, here it is:
    Tested on XP, java 1.4.2, VC++ 6.0.
    ************ File invoke.cpp *****************************
    #include <stdlib.h>
    #include <jni.h>
    #ifdef _WIN32
    #define PATH_SEPARATOR ';'
    #else /* UNIX */
    #define PATH_SEPARATOR ':'
    #endif
    #define USER_CLASSPATH "." /* where Prog.class is */
    void JNICALL displayHelloWorld(JNIEnv *env, jobject obj)
        printf("Hello world: made by printf from C++\n");
        return;
    void main() {
        JNIEnv *env;
        JavaVM *jvm;
        JavaVMInitArgs vm_args;
        jint res;
        jclass cls;
        jmethodID mid;
        jstring jstr;
        jobjectArray args;
        char classpath[1024];
         int result;
        /* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
         JavaVMOption options[3];
         options[0].optionString = "-verbose:gc";
         sprintf (classpath, "-Djava.class.path=%s", USER_CLASSPATH);
         options[1].optionString = classpath;
         options[2].optionString = "-Djava.compiler=NONE";
         vm_args.options = options;
         vm_args.nOptions = 3;
         vm_args.ignoreUnrecognized = JNI_TRUE;
            vm_args.version = JNI_VERSION_1_4;
         /* IMPORTANT: Note that in the Java 2 SDK, there is no longer any need to call
          * JNI_GetDefaultJavaVMInitArgs. It causes a bug
        /* Append USER_CLASSPATH to the end of default system class path */
        /* Create the Java VM */
        res = JNI_CreateJavaVM(&jvm,(void**)&env, &vm_args);
        if (res < 0) {
            fprintf(stderr, "Can't create Java VM\n");
            exit(1);
        cls = env->FindClass("mypackage/Prog");
        if (cls == 0) {
            fprintf(stderr, "Can't find mypackage/Prog class\n");
            exit(1);
       JNINativeMethod nm;
       nm.name = "displayHelloWorld";
       /* method descriptor assigned to signature field */
       nm.signature = "()V";
       nm.fnPtr = displayHelloWorld;
       env->RegisterNatives(cls, &nm, 1);
        mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
        if (mid == 0) {
            fprintf(stderr, "Can't find Prog.main\n");
            exit(1);
        jstr = env->NewStringUTF(" from C++! calleded by C++, using argument from C++ but java method");
        if (jstr == 0) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        args = env->NewObjectArray(1,
                            env->FindClass("java/lang/String"), jstr);
        if (args == 0) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        env->CallStaticVoidMethod(cls, mid, args);
        jvm->DestroyJavaVM();
    }********************* File ./mypackage/Prog.java **************************
    package mypackage;
    public class Prog {
           public native void displayHelloWorld();
        public static void main(String[] args) {
            System.out.println("Hello World" + args[0] +"\n");
            Prog prog = new Prog();
            prog.displayHelloWorld();
            System.out.println("(called from java)");
            System.out.println("\nSo to sumurize:");
            System.out.println(" -1 Starting point is an exe (not DLL, there is no DLL) so C++ code");
            System.out.println(" -2 From C++ call java method using argument from C++");
            System.out.println(" -3 From java, that was launched by the exe, call to");
            System.out.println("    a native using the RegisterNatives in C++\n");
            System.out.println("You got a bidirectional example with as starting point");
            System.out.println("a single exe, launching JVM, loading class call back C++!");
    }******************* Command line for all ****************************************
    javac mypackage/Prog
    cl -I"D:\Program Files\j2sdk1.4.2_03\include" -I"D:\Program Files\j2sdk1.4.2_03\include\win32" -MT
    invoke.cpp -link D:\PROGRA~1\j2sdk1.4.2_03\lib\jvm.lib
    (Remark, the last path is using the short name for "Program Files" because with the blank
    even adding double quotes result into an error)
    You must have jvm.dll in your path for me it's Path=D:\Program Files\j2sdk1.4.2_03\jre\bin\client;
    Then simply call invoke and see the result. :-)

  • To call a VB dll from java

    Hi,
    I want to know how one can call a VB dll from Java.I know it is something like calling a C dll which in turn calls the VB dll.I also went through the tutorial provided on the sun site and was able to call a C dll through java but I still do not know how to call a VB dll from java.
    regards,
    Anshuman

    Have you checked google?
    First hit. Reply 17.

  • Calling a PL/SQL function from java

    I would like to call a pl/sql function from java. My pl/sql function is taking arrays of records as parameters. How do i proceed? Which specific oracle packages do I have to import?
    Please send an example.
    TIA,
    Darko Guberina

    Documentation here: http://download-uk.oracle.com/docs/cd/B14117_01/java.101/b10983/datamap.htm#sthref185
    says JPublisher can publish records too.
    But when I change the example given at http://download-uk.oracle.com/docs/cd/B14117_01/java.101/b10983/datamap.htm#sthref190 as following:
    PACKAGE "COMPANY" AS
    type emp_rec is record (empno number, ename varchar2(10));
    type emp_list is varray(5) of emp_rec;
    type factory is record (
    name varchar(10),
    emps emp_list
    function get_factory(p_name varchar) return factory;
    END;
    then I see <unknown type or type not found> at sql or java files generated. Any ideas?

  • Calling native Objective C code from Java Script

    Hi,
    I want to call native objective C code from Java script.
    I know one way by using : webView:shouldStartLoadWithRequest:navigationType
    Is there another way of doing same?
    Thanks,
    Ganesh Pisal
    Vavni Inc

    Are any of those threads calling java code? If yes then are you calling the Attach method?

  • Calling C prog. from Java prog.

    Hi.
    I have a java GUI program as a interface. User can enter data, then java prog calls a C program and at the end the java GUI shows the returned parameters of C prog. The question is: how can I call C prog from Java GUI?
    Thanks

    "Calling a program" is not standard terminology, so I'm not sure what you're asking.
    Do you want to call a function in a DLL (most likely written in C) inside your Java code? Yes, that's JNI.
    Or do you want to execute a program (which may happen to have been written in C, but the language doesn' matter) from inside you Java code? That's [Runtime.exec|http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html].

  • Call a method in VB dll from Java Web Application

    Hi,
    I'm trying to call a method of a VB dll, from a web application developing with J2EE. Is it possible without JNI? And, if it is not possible with a tool, can you help me with an example of JNI using?
    Thank you
    Mary

    maria_eg wrote:
    I'm trying to call a method of a VB dll, from a web application developing with J2EE. Is it possible without JNI? Maybe using Runtime#exec() and rundll32.exe. Depends on the DLL which you want to call.
    And, if it is not possible with a tool, can you help me with an example of JNI using?JNI tutorial: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
    JNI forum: http://forum.java.sun.com/forum.jspa?forumID=52

  • How to call dll from Java

    Hi everybody,
    I am trying to call a dll from my java application. Pls tell me how to do it, I seached and found JNI. Is it true
    pls help me and send me some examples
    Thank you and have a nice day

    java_and_me wrote:
    I am trying to call a dll from my java application. Pls tell me how to do it, I seached and found JNI. Is it trueYes, it is true. JNI is the way to go. You'll probably have to write some C code on your own as well, as the DLL is unlikely to be in the format expected by Java already.
    pls help me and send me some examplesOf course. [Here you are|http://www.lmgtfy.com/?q=JNI+tutorial].

  • Call a C# dll from java is work only with the machine which is build C# dll

    hi all,
    I've built a VC++(win32 console dll app)..
    Created a Java program to access the said VC++ dll
    It was successful in the machine(say machine 1) which is created the VC++ dll..But Not work with machine2
    n.b VC++ dll is accessing another VC#dll
    If I combile the VC++ dll in machine2 then then its works fine with machine2 while access from java But not work with machine1
    Problem comes when I run the same application in a different system where all the
    needed visual studio files are installed...
    I tried by create the VC++ dll with skipping the part of accessing VC# dll, then it works fine with anotther machine
    NB: error accurs only when it is run on a different machine...
    Error is:
    # An unexpected error has been detected by HotSpot Virtual Machine:
    # Internal Error (0xe06d7363), pid=4024, tid=4028
    # Java VM: Java HotSpot(TM) Client VM (1.5.0_09-b01 mixed mode, sharing)
    # Problematic frame:
    # C [kernel32.dll+0x1eb33]
    # An error report file with more information is saved as hs_err_pid4024.log
    # If you would like to submit a bug report, please visit:
    # http://java.sun.com/webapps/bugreport/crash.jsp
    i've done "googling" with this error... but no one has come up with some satisfactory answer...
    Pls help.....
    conf. of both machine
    Windows XP
    jdk1.5.0_09, jre1.5.0_09
    thanks in advance..

    Normally any system exception that occurs when working with JNI is caused by one of the following.
    - pointer problems
    - incorrect usage of another api
    - bug in the api being used.
    The last two are often still pointer problems.
    The fact that it ones one machine and not another makes it more likely it is a pointer problem (in one or more of the above.)
    A pointer problem cause cause the system to fail long after the point where the bug actually is (coded.) Different execution paths, such as running on different machines, means the bug will manifest itself differently.

  • Calling stored procedures in Sybase from java

    Hi,
    I am using the following stored procedure in Sybase
    use xyzdb
    go
    -- drop procedure if it already exist
    if object_id('up_name_select') is not null
    begin
    drop procedure up_name_select
    end
    go
    create procedure up_name_select
    @zid          numeric(7,0),
    @firstname     char(40),
    @lastname     char(40)
    as
    select zid,
    firstname,
    lastname
    from name
    where zid = @zid or
    (lastname like @lastname or firstname like @firstname)
    go
    -- update documentation records in object_docs
    delete object_docs
    from object_docs
    where object_name = "up_name_select"
    go
    insert into object_docs values("up_name_select","Selects records from the name table based upon the values of the input parameters.")
    go
    -- update documentation records in column_docs
    delete column_docs
    from column_docs
    where object_name = "up_name_select"
    go
    insert into column_docs values("up_name_select","@zid","System generated ID for an individual contact.")
    insert into column_docs values("up_name_select","@firstname","First name of the contact. SQL wild card characters are accepted.")
    insert into column_docs values("up_name_select","@lastname","Last name of the contact. SQL wild card characters are accepted.")
    go
    -- print success message and grant permissions
    if object_id('up_name_select') is not null
    begin
    print "Procedure up_name_select created."
    grant execute on up_name_select to developer_role
    end
    go
    This stored procedure selects the values from the table "name" for a given where condition (if I am not wrong).
    Can any one give me sample java code to select the records from the table "name" for a given zid.
    Thankyou in advance.
    Regards
    sgatl2

    calling stored procedures from java
    here is the sample code
    CallableStatement cs = con.prepareCall("{call selectlogin (?)}");
    cs.setString (1, "value");         
    ResultSet rs = cs.executeQuery ();
                while (rs.next ())
                //your code for display
                } more on gooooooogle
    http://www.google.com/search?q=calling+stored+procedures+from+java+with+sample+example&client=netscape-pp&rls=com.netscape:en-US

  • Calling Functions of Loading DLL using java

    I have a dll file
    I know the functions which are ther in the dll
    But how to call those functions using JAVA ?
    can anyone tell me how to do the same...
    Thanx in advance
    regards,
    Ritesh

    I assume that you have a regular DLL and that the functions in that DLL are exported and that you know the signatures of each function.
    With that in mind, you will need to use the Java Native Interface (JNI), to call those functions from Java. In a nutshell, you will need to create at least one class which declares some native methods, code a class static code block to load the JNI-DLL you are about to create, compile the class(es), run the javah tool supplied with the JDK which will emit a C compatible header file, implement the functions from the generated header file and compile that source file to into a DLL. It would be in this DLL that you would use the functions in the DLL that you ultimately want to access. So, what you wind up doing is creating a wrapper DLL.
    Take a look at the JNI tutorial trail from Sun at http://java.sun.com/docs/books/tutorial/native1.1/index.html.

  • Call JavaScript method in FXML from java controller

    I have fxml like
       <fx:root type="javafx.scene.Group" xmlns:fx="http://javafx.com/fxml">
        <fx:script>
            function applyState(oldState, newState)
        </fx:script>   
        ....and controller to it.
    The idea is to move some view logic to fxml file.
    So, when I need to change some view state, I want to call applyState from java code.
    The question is how to do it.
    What I have found:
    We can get
    fxmlLoader.getNamespace().get("applyState")and receive sun.org.mozilla.javascript.internal.InterpretedFunction.
    NetBeans see this class. But while building the project i have an error
    error: package sun.org.mozilla.javascript.internal does not exist
    But this class really exists in rt.jar in JRE.
    After that I have stopped digging into this.
    I suspect that using internal API is not a good idea to call this InterpretedFunction.
    Can somebody suggest how can I make such an invocation?
    Edited by: 940811 on Nov 19, 2012 11:21 PM

    Until JavaFX doesn't expose the ScriptEngine instance of FXMLLoader, there's no way to communicate (Java <-> Javascript) with the <fx:script>.
    But, if you want to rely on a hack, you can do this:
        private ScriptEngine extractScriptEngine(FXMLLoader loader) {
            try {
                Field fse = loader.getClass().getDeclaredField("scriptEngine");
                fse.setAccessible(true);
                return (ScriptEngine) fse.get(loader);
            } catch (IllegalAccessException | NoSuchFieldException | SecurityException ex) {
                Logger.getLogger(BrowserFXController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

  • How to access data structures in C dll from java thru JNI?

    We have been given API's( collection of C Functions) from some vendor.
    SDK from vendor consist of:
    Libpga.DLL, Libpga.h,Libpga.lib, Along with that sample program Receiver.h (i don't know its written in C or C++), I guess .C stnads for C files?
    Considering that I don't know C or C++ (Except that I can understand what that program is doing) & i have experience in VB6 and Java, In order to build interface based on this API, I have two option left, Use these dll either from VB or Java.
    As far as I know, calling this DLL in VB requires all the data structures & methods to be declared in VB, I guess which is not the case with Java (? I'm not sure)
    I experiemnted calling these function from Java through JNI, and I successfully did by writting wrapper dll. My question is whether I have to declare all the constants & data structures defined in libpga.h file in java, in order to use them in my java program??
    Any suggesstion would be greatly appreciated,
    Vini

    1. There are generators around that claim to generate suitable wrappers, given some dll input. I suggest you search google. Try JACE, jni, wrapper, generator, .... Also, serach back through this forum, where there have been suggestions made.
    2. In general, you will need to supply wrappers, and if you want to use data from the "C side" in java, then you will need java objects that hold the data.

Maybe you are looking for

  • Whatsapp is not working in iphone 3g. what should i do.

    whatsapp is not working in iphone 3g. what should i do.

  • PDF on iCloud

    I am avery happy user on MobileMe (with its iDisk feature) from more than 2 years. I now moved to iCloud and I can not sync my PDF (and other) files anymore, but I can only sync pages, numbers and keynotes files. It is completely useless! Is anyone a

  • Screenshots

    Despite the advice received I cant insert any screenshot to mi iBook through iTunes producer, all the time I get an error message "the picture doesnt match the requierements". The last one I try to put a picture sized 1024 X 720 and also 800 X600 but

  • Creating links between movies

    Hi everyone, I'm an iDVD noob, so my apologies if these questions were already asked (though I did do a search). I have iDVD 7.1.2 on my Mac Pro (OS 10.6.8) My first question is- can I create a custom theme from scratch in iDVD (can be a still), or d

  • Why can't I export my newsletter from CS into a pdf file all of a sudden?

    Why can't I export my newsletter from CS into a psf file all of a sudden?