Loading a 3rd party from a java code

Discussion in 'Java' started by dvir shaty, Feb 13, 2011.

  1. dvir shaty

    dvir shaty Guest

    Hi all,

    I am trying to load a function from a 3rd party DLL from my java code
    (in eclipse).

    Can you please tell me how to do it?

    I tried to use the following:

    ------------------------------------------------
    package tal.packege;
    class talclient
    {
    static
    {
    System.loadLibrary("talclient");
    }
    public static void main(String ar[])
    {
    System.out.println("Hello world from Java");
    talclient t=new talclient();
    String s = "172.16.10.31";
    int a = 8888;
    int b = 0;
    t.tal_connect(s,a,b);

    }
    public native void tal_connect(String s, int a, int b);
    }

    ------------------------------------------------


    I get the following exception:


    Exception in thread "main" java.lang.UnsatisfiedLinkError:
    tal.packege.talclient.vtc_connect(Ljava/lang/String;II)V
    at tal.packege.talclient.vtc_connect(Native Method)
    at tal.packege.talclient.main(talclient.java:16)

    According to the dll help file the arguments are defined as:

    int tal_connect( char *servername,
    Int portnum,
    Int options)

    The dll is stored in c:\windows\system32.


    Thanks, Dvir.
    dvir shaty, Feb 13, 2011
    #1
    1. Advertising

  2. dvir shaty

    dvir shaty Guest

    To make things clearer, the DLL is a C DLL.
    dvir shaty, Feb 13, 2011
    #2
    1. Advertising

  3. On 02/13/2011 03:42 AM, dvir shaty wrote:
    > According to the dll help file the arguments are defined as:
    >
    > int tal_connect( char *servername,
    > Int portnum,
    > Int options)


    How does Java know that? DLLs would just store the exported function as
    "tal_connect", with no type information whatsoever, so Java has no clue
    what the type information is.

    If it is just a raw C DLL, you will have to write the native JNI code to
    do that. Googling `JNI tutorial' should give it to you.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Feb 13, 2011
    #3
  4. dvir shaty

    Arne Vajhøj Guest

    On 13-02-2011 03:42, dvir shaty wrote:
    > I am trying to load a function from a 3rd party DLL from my java code
    > (in eclipse).
    >
    > Can you please tell me how to do it?
    >
    > I tried to use the following:
    >
    > ------------------------------------------------
    > package tal.packege;
    > class talclient
    > {
    > static
    > {
    > System.loadLibrary("talclient");
    > }
    > public static void main(String ar[])
    > {
    > System.out.println("Hello world from Java");
    > talclient t=new talclient();
    > String s = "172.16.10.31";
    > int a = 8888;
    > int b = 0;
    > t.tal_connect(s,a,b);
    >
    > }
    > public native void tal_connect(String s, int a, int b);
    > }
    >
    > ------------------------------------------------
    >
    >
    > I get the following exception:
    >
    >
    > Exception in thread "main" java.lang.UnsatisfiedLinkError:
    > tal.packege.talclient.vtc_connect(Ljava/lang/String;II)V
    > at tal.packege.talclient.vtc_connect(Native Method)
    > at tal.packege.talclient.main(talclient.java:16)
    >
    > According to the dll help file the arguments are defined as:
    >
    > int tal_connect( char *servername,
    > Int portnum,
    > Int options)
    >
    > The dll is stored in c:\windows\system32.


    JNI does not support calling arbitrary functions in a
    DLL.

    You need to create wrapper function and DLL that is
    JNI compatible and call that.

    Arne
    Arne Vajhøj, Feb 13, 2011
    #4
  5. dvir shaty

    Ian Shef Guest

    Arne Vajhøj <> wrote in news:4d580572$0$23756$14726298
    @news.sunsite.dk:

    > On 13-02-2011 03:42, dvir shaty wrote:
    >> I am trying to load a function from a 3rd party DLL from my java code
    >> (in eclipse).
    >>
    >> Can you please tell me how to do it?
    >>
    >> I tried to use the following:
    >>
    >> ------------------------------------------------
    >> package tal.packege;
    >> class talclient
    >> {
    >> static
    >> {
    >> System.loadLibrary("talclient");
    >> }
    >> public static void main(String ar[])
    >> {
    >> System.out.println("Hello world from Java");
    >> talclient t=new talclient();
    >> String s = "172.16.10.31";
    >> int a = 8888;
    >> int b = 0;
    >> t.tal_connect(s,a,b);
    >>
    >> }
    >> public native void tal_connect(String s, int a, int b);
    >> }
    >>
    >> ------------------------------------------------
    >>
    >>
    >> I get the following exception:
    >>
    >>
    >> Exception in thread "main" java.lang.UnsatisfiedLinkError:
    >> tal.packege.talclient.vtc_connect(Ljava/lang/String;II)V
    >> at tal.packege.talclient.vtc_connect(Native Method)
    >> at tal.packege.talclient.main(talclient.java:16)
    >>
    >> According to the dll help file the arguments are defined as:
    >>
    >> int tal_connect( char *servername,
    >> Int portnum,
    >> Int options)
    >>
    >> The dll is stored in c:\windows\system32.

    >
    > JNI does not support calling arbitrary functions in a
    > DLL.
    >
    > You need to create wrapper function and DLL that is
    > JNI compatible and call that.
    >
    > Arne
    >

    And furthermore...
    - Your code calls tal_connect, but the error message is for vtc_connect.
    - char * is NOT the same as java.lang.String
    Ian Shef, Feb 15, 2011
    #5
  6. dvir shaty

    Arne Vajhøj Guest

    On 15-02-2011 18:35, Ian Shef wrote:
    > Arne Vajhøj<> wrote in news:4d580572$0$23756$14726298
    > @news.sunsite.dk:
    >
    >> On 13-02-2011 03:42, dvir shaty wrote:
    >>> I am trying to load a function from a 3rd party DLL from my java code
    >>> (in eclipse).
    >>>
    >>> Can you please tell me how to do it?
    >>>
    >>> I tried to use the following:
    >>>
    >>> ------------------------------------------------
    >>> package tal.packege;
    >>> class talclient
    >>> {
    >>> static
    >>> {
    >>> System.loadLibrary("talclient");
    >>> }
    >>> public static void main(String ar[])
    >>> {
    >>> System.out.println("Hello world from Java");
    >>> talclient t=new talclient();
    >>> String s = "172.16.10.31";
    >>> int a = 8888;
    >>> int b = 0;
    >>> t.tal_connect(s,a,b);
    >>>
    >>> }
    >>> public native void tal_connect(String s, int a, int b);
    >>> }
    >>>
    >>> ------------------------------------------------
    >>>
    >>>
    >>> I get the following exception:
    >>>
    >>>
    >>> Exception in thread "main" java.lang.UnsatisfiedLinkError:
    >>> tal.packege.talclient.vtc_connect(Ljava/lang/String;II)V
    >>> at tal.packege.talclient.vtc_connect(Native Method)
    >>> at tal.packege.talclient.main(talclient.java:16)
    >>>
    >>> According to the dll help file the arguments are defined as:
    >>>
    >>> int tal_connect( char *servername,
    >>> Int portnum,
    >>> Int options)
    >>>
    >>> The dll is stored in c:\windows\system32.

    >>
    >> JNI does not support calling arbitrary functions in a
    >> DLL.
    >>
    >> You need to create wrapper function and DLL that is
    >> JNI compatible and call that.


    > And furthermore...
    > - Your code calls tal_connect, but the error message is for vtc_connect.
    > - char * is NOT the same as java.lang.String


    Both will somewhat become obvious when creating the
    JNI wrapper.

    The output from javah will show a lot.

    Arne
    Arne Vajhøj, Feb 16, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry McKiernan
    Replies:
    0
    Views:
    1,349
    Terry McKiernan
    Jul 7, 2003
  2. BestNews

    Any 3rd party Grid control in dotnet

    BestNews, Aug 26, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    5,149
    Robert Jacobson
    Aug 26, 2003
  3. Matalote
    Replies:
    2
    Views:
    436
    Matalote
    Jan 26, 2004
  4. 2xzero
    Replies:
    1
    Views:
    365
    znôrt
    Feb 16, 2004
  5. buck
    Replies:
    10
    Views:
    164
    Uri Guttman
    Feb 18, 2010
Loading...

Share This Page